3364. Minimum Positive Sum Subarray LeetCode Solution
In this guide, you will get 3364. Minimum Positive Sum Subarray LeetCode Solution with the best time and space complexity. The solution to Minimum Positive Sum Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Positive Sum Subarray solution in C++
- Minimum Positive Sum Subarray solution in Java
- Minimum Positive Sum Subarray solution in Python
- Additional Resources

Problem Statement of Minimum Positive Sum Subarray
You are given an integer array nums and two integers l and r. Your task is to find the minimum sum of a subarray whose size is between l and r (inclusive) and whose sum is greater than 0.
Return the minimum sum of such a subarray. If no such subarray exists, return -1.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3, -2, 1, 4], l = 2, r = 3
Output: 1
Explanation:
The subarrays of length between l = 2 and r = 3 where the sum is greater than 0 are:
[3, -2] with a sum of 1
[1, 4] with a sum of 5
[3, -2, 1] with a sum of 2
[-2, 1, 4] with a sum of 3
Out of these, the subarray [3, -2] has a sum of 1, which is the smallest positive sum. Hence, the answer is 1.
Example 2:
Input: nums = [-2, 2, -3, 1], l = 2, r = 3
Output: -1
Explanation:
There is no subarray of length between l and r that has a sum greater than 0. So, the answer is -1.
Example 3:
Input: nums = [1, 2, 3, 4], l = 2, r = 4
Output: 3
Explanation:
The subarray [1, 2] has a length of 2 and the minimum sum greater than 0. So, the answer is 3.
Constraints:
1 <= nums.length <= 100
1 <= l <= r <= nums.length
-1000 <= nums[i] <= 1000
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(1)
3364. Minimum Positive Sum Subarray LeetCode Solution in C++
class Solution {
public:
int minimumSumSubarray(vector<int>& nums, int l, int r) {
int ans = INT_MAX;
for (int windowSize = l; windowSize <= r; ++windowSize) {
int sum = accumulate(nums.begin(), nums.begin() + windowSize, 0);
if (sum > 0)
ans = min(ans, sum);
for (int i = windowSize; i < nums.size(); ++i) {
sum -= nums[i - windowSize];
sum += nums[i];
if (sum > 0)
ans = min(ans, sum);
}
}
return ans == INT_MAX ? -1 : ans;
}
};
/* code provided by PROGIEZ */
3364. Minimum Positive Sum Subarray LeetCode Solution in Java
class Solution {
public int minimumSumSubarray(List<Integer> nums, int l, int r) {
int ans = Integer.MAX_VALUE;
for (int windowSize = l; windowSize <= r; ++windowSize) {
int sum = 0;
for (int i = 0; i < windowSize; ++i)
sum += nums.get(i);
if (sum > 0)
ans = Math.min(ans, sum);
for (int i = windowSize; i < nums.size(); ++i) {
sum -= nums.get(i - windowSize);
sum += nums.get(i);
if (sum > 0)
ans = Math.min(ans, sum);
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}
// code provided by PROGIEZ
3364. Minimum Positive Sum Subarray LeetCode Solution in Python
class Solution:
def minimumSumSubarray(self, nums: list[int], l: int, r: int) -> int:
ans = math.inf
for windowSize in range(l, r + 1):
windowSum = sum(nums[:windowSize])
if windowSum > 0:
ans = min(ans, windowSum)
for i in range(windowSize, len(nums)):
windowSum -= nums[i - windowSize]
windowSum += nums[i]
if windowSum > 0:
ans = min(ans, windowSum)
return -1 if ans == math.inf else ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.