3364. Minimum Positive Sum Subarray
Problem Description
You are given an integer array nums
and two integers l
and r
. Your task is to find a subarray that satisfies the following conditions:
- The subarray must have a length between
l
andr
(inclusive) - The sum of the subarray must be greater than 0
- Among all such valid subarrays, you need to find the one with the minimum sum
A subarray is defined as a contiguous non-empty sequence of elements from the original array. For example, if nums = [1, 2, 3]
, then [1]
, [2, 3]
, and [1, 2, 3]
are all subarrays, but [1, 3]
is not because it's not contiguous.
The function should return the minimum sum of such a subarray. If no subarray exists that meets all the criteria, return -1
.
Example scenarios:
- If
nums = [3, -2, 1, 4]
,l = 2
,r = 3
, you need to find subarrays of length 2 or 3 whose sum is positive, then return the smallest such sum. - The subarray length is calculated as
right_index - left_index + 1
. - The sum must be strictly greater than 0 (not equal to 0).
Intuition
Since we need to find the minimum sum among all valid subarrays, and the constraints involve both the length of the subarray and its sum being positive, we need to examine all possible subarrays that could potentially be our answer.
The key insight is that we cannot use typical optimization techniques like sliding window or dynamic programming here because:
- We're looking for the minimum positive sum, not maximum
- The array can contain negative numbers, which means subarrays don't have a monotonic property
This leads us to a brute force approach: we need to check every possible subarray whose length falls within [l, r]
.
For each starting position i
, we can build subarrays incrementally by extending the right endpoint j
. As we extend, we maintain a running sum s
by adding nums[j]
to it. This is more efficient than recalculating the sum from scratch for each subarray.
Once we have a subarray of length at least l
(when j - i + 1 >= l
), we start checking if it meets our criteria:
- Is the length within bounds? (
l <= j - i + 1 <= r
) - Is the sum positive? (
s > 0
)
If both conditions are met, we update our answer with the minimum between the current answer and this sum.
The reason this works is that we're guaranteed to explore all possible subarrays of valid lengths, and by keeping track of the minimum positive sum we encounter, we'll find the optimal answer. If no valid subarray exists after checking all possibilities, we return -1
.
Learn more about Prefix Sum and Sliding Window patterns.
Solution Approach
The solution uses a nested loop approach to enumerate all possible subarrays:
-
Outer Loop - Starting Position: We iterate through each index
i
from0
ton-1
as the starting position of our subarray. -
Inner Loop - Ending Position: For each starting position
i
, we iterate through each indexj
fromi
ton-1
as the ending position. This ensures we examine all subarrays starting at positioni
. -
Running Sum Calculation: We maintain a running sum
s
that starts at0
for each new starting position. As we extend the subarray by movingj
, we addnums[j]
tos
. This gives us the sum of the subarray[i, j]
in constant time. -
Validation Check: For each subarray
[i, j]
, we check two conditions:- Length constraint:
l <= j - i + 1 <= r
(the subarray length must be betweenl
andr
) - Positive sum:
s > 0
(the sum must be strictly positive)
- Length constraint:
-
Answer Update: If both conditions are satisfied, we update our answer with
min(ans, s)
, keeping track of the minimum positive sum found so far. -
Initialization and Return:
- We initialize
ans
with infinity (inf
) to ensure any valid sum will be smaller - After examining all subarrays, if
ans
is still infinity, it means no valid subarray was found, so we return-1
- Otherwise, we return the minimum sum found
- We initialize
The time complexity is O(n²)
where n
is the length of the array, as we examine all possible subarrays. The space complexity is O(1)
as we only use a few variables to track the current state.
Ready to land your dream job?
Unlock your dream job with a 3-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through the solution with a concrete example:
nums = [3, -2, 1, 4]
l = 2
,r = 3
- We need subarrays of length 2 or 3 with positive sum, and want the minimum such sum.
Step-by-step execution:
Starting at i = 0 (nums[0] = 3):
- j = 0: subarray [3], length = 1, sum = 3
- Length 1 < l (2), so skip
- j = 1: subarray [3, -2], length = 2, sum = 3 + (-2) = 1
- Length 2 ∈ [2, 3] ✓, sum 1 > 0 ✓
- Update ans = min(∞, 1) = 1
- j = 2: subarray [3, -2, 1], length = 3, sum = 1 + 1 = 2
- Length 3 ∈ [2, 3] ✓, sum 2 > 0 ✓
- Update ans = min(1, 2) = 1
- j = 3: subarray [3, -2, 1, 4], length = 4, sum = 2 + 4 = 6
- Length 4 > r (3), so skip
Starting at i = 1 (nums[1] = -2):
- j = 1: subarray [-2], length = 1, sum = -2
- Length 1 < l (2), so skip
- j = 2: subarray [-2, 1], length = 2, sum = -2 + 1 = -1
- Length 2 ∈ [2, 3] ✓, but sum -1 ≤ 0 ✗, so skip
- j = 3: subarray [-2, 1, 4], length = 3, sum = -1 + 4 = 3
- Length 3 ∈ [2, 3] ✓, sum 3 > 0 ✓
- Update ans = min(1, 3) = 1
Starting at i = 2 (nums[2] = 1):
- j = 2: subarray [1], length = 1, sum = 1
- Length 1 < l (2), so skip
- j = 3: subarray [1, 4], length = 2, sum = 1 + 4 = 5
- Length 2 ∈ [2, 3] ✓, sum 5 > 0 ✓
- Update ans = min(1, 5) = 1
Starting at i = 3 (nums[3] = 4):
- j = 3: subarray [4], length = 1, sum = 4
- Length 1 < l (2), so skip
Result: The minimum positive sum among all valid subarrays is 1, achieved by the subarray [3, -2] at indices [0, 1].
Solution Implementation
1class Solution:
2 def minimumSumSubarray(self, nums: List[int], l: int, r: int) -> int:
3 n = len(nums)
4 min_sum = float('inf') # Initialize minimum sum to infinity
5
6 # Try all possible starting positions for subarrays
7 for start in range(n):
8 current_sum = 0
9
10 # Extend the subarray from the starting position
11 for end in range(start, n):
12 current_sum += nums[end]
13 subarray_length = end - start + 1
14
15 # Check if subarray meets the conditions:
16 # 1. Length is between l and r (inclusive)
17 # 2. Sum is positive
18 if l <= subarray_length <= r and current_sum > 0:
19 min_sum = min(min_sum, current_sum)
20
21 # Return -1 if no valid subarray found, otherwise return minimum sum
22 return -1 if min_sum == float('inf') else min_sum
23
1class Solution {
2 /**
3 * Finds the minimum sum of a subarray with positive sum and length between l and r.
4 *
5 * @param nums The input list of integers
6 * @param l The minimum allowed subarray length
7 * @param r The maximum allowed subarray length
8 * @return The minimum positive sum of valid subarrays, or -1 if no such subarray exists
9 */
10 public int minimumSumSubarray(List<Integer> nums, int l, int r) {
11 int n = nums.size();
12 final int INF = Integer.MAX_VALUE;
13 int minSum = INF;
14
15 // Iterate through all possible starting positions
16 for (int start = 0; start < n; start++) {
17 int currentSum = 0;
18
19 // Extend the subarray from the current starting position
20 for (int end = start; end < n; end++) {
21 // Add current element to the running sum
22 currentSum += nums.get(end);
23
24 // Calculate the current subarray length
25 int subarrayLength = end - start + 1;
26
27 // Check if subarray meets all conditions:
28 // 1. Length is within the range [l, r]
29 // 2. Sum is positive
30 if (subarrayLength >= l && subarrayLength <= r && currentSum > 0) {
31 // Update minimum sum if current sum is smaller
32 minSum = Math.min(minSum, currentSum);
33 }
34 }
35 }
36
37 // Return -1 if no valid subarray was found, otherwise return the minimum sum
38 return minSum == INF ? -1 : minSum;
39 }
40}
41
1class Solution {
2public:
3 int minimumSumSubarray(vector<int>& nums, int l, int r) {
4 int n = nums.size();
5 const int INF = INT_MAX;
6 int minSum = INF;
7
8 // Iterate through all possible starting positions
9 for (int start = 0; start < n; ++start) {
10 int currentSum = 0;
11
12 // Extend the subarray from the current starting position
13 for (int end = start; end < n; ++end) {
14 currentSum += nums[end];
15 int subarrayLength = end - start + 1;
16
17 // Check if the subarray meets the criteria:
18 // 1. Length is within the range [l, r]
19 // 2. Sum is positive
20 if (subarrayLength >= l && subarrayLength <= r && currentSum > 0) {
21 minSum = min(minSum, currentSum);
22 }
23 }
24 }
25
26 // Return -1 if no valid subarray was found, otherwise return the minimum sum
27 return minSum == INF ? -1 : minSum;
28 }
29};
30
1/**
2 * Finds the minimum sum of a subarray with length between l and r (inclusive) that has a positive sum.
3 * @param nums - The input array of numbers
4 * @param l - The minimum length of the subarray
5 * @param r - The maximum length of the subarray
6 * @returns The minimum positive sum of a valid subarray, or -1 if no such subarray exists
7 */
8function minimumSumSubarray(nums: number[], l: number, r: number): number {
9 const arrayLength: number = nums.length;
10 let minimumSum: number = Infinity;
11
12 // Iterate through all possible starting positions
13 for (let startIndex: number = 0; startIndex < arrayLength; startIndex++) {
14 let currentSum: number = 0;
15
16 // Extend the subarray from the current starting position
17 for (let endIndex: number = startIndex; endIndex < arrayLength; endIndex++) {
18 currentSum += nums[endIndex];
19 const subarrayLength: number = endIndex - startIndex + 1;
20
21 // Check if the subarray meets the length constraints and has a positive sum
22 if (subarrayLength >= l && subarrayLength <= r && currentSum > 0) {
23 minimumSum = Math.min(minimumSum, currentSum);
24 }
25 }
26 }
27
28 // Return -1 if no valid subarray was found, otherwise return the minimum sum
29 return minimumSum === Infinity ? -1 : minimumSum;
30}
31
Time and Space Complexity
The time complexity is O(n^2)
, where n
is the length of the array nums
. This is because we have two nested loops: the outer loop iterates through all possible starting positions i
from 0
to n-1
, and for each starting position, the inner loop iterates through all possible ending positions j
from i
to n-1
. In the worst case, this results in approximately n * (n+1) / 2
iterations, which simplifies to O(n^2)
.
The space complexity is O(1)
. The algorithm only uses a constant amount of extra space for variables n
, ans
, i
, j
, and s
, regardless of the input size. No additional data structures that grow with the input size are created.
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Confusing Minimum Positive Sum with Overall Minimum
A common mistake is trying to find the overall minimum sum first and then checking if it's positive. This approach fails because the minimum sum subarray might be negative, while there could be other positive-sum subarrays that are valid.
Incorrect approach:
# Wrong: Finding minimum sum first, then checking if positive
min_sum = float('inf')
for start in range(n):
current_sum = 0
for end in range(start, n):
current_sum += nums[end]
if l <= end - start + 1 <= r:
min_sum = min(min_sum, current_sum)
return min_sum if min_sum > 0 else -1 # This misses valid positive subarrays!
Correct approach: Only consider subarrays with positive sums when updating the minimum.
2. Early Termination When Sum Becomes Negative
Another pitfall is breaking the inner loop when the current sum becomes negative, thinking no further extensions will be valid. This is wrong because adding more elements might make the sum positive again.
Incorrect approach:
for start in range(n):
current_sum = 0
for end in range(start, n):
current_sum += nums[end]
if current_sum <= 0:
break # Wrong! Future elements might make it positive
Example: nums = [-1, -2, 10]
, l = 3
, r = 3
- If we break when sum becomes negative, we miss the valid subarray
[-1, -2, 10]
with sum = 7
3. Not Handling Edge Cases Properly
Failing to initialize the minimum sum correctly or not checking if any valid subarray was found can lead to incorrect outputs.
Common mistakes:
- Initializing
min_sum
to 0 instead of infinity - Returning 0 or the unmodified infinity value instead of -1 when no valid subarray exists
- Not considering that all subarrays within the length range might have non-positive sums
Solution: Always initialize with float('inf')
and explicitly check if it remains unchanged before returning the result.
Which type of traversal does breadth first search do?
Recommended Readings
Prefix Sum The prefix sum is an incredibly powerful and straightforward technique Its primary goal is to allow for constant time range sum queries on an array What is Prefix Sum The prefix sum of an array at index i is the sum of all numbers from index 0 to i By
https assets algo monster cover_photos stack svg Sliding Window Maximum Monotonic Stack We have an array and a sliding window defined by a start index and an end index The sliding window moves from left of the array to right There are always k elements in the window The window
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Want a Structured Path to Master System Design Too? Don’t Miss This!