Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 3Out 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 <= 1001 <= l <= r <= nums.length-1000 <= nums[i] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[3,-2,1,4] 2 3
[-2,2,-3,1] 2 3
[1,2,3,4] 2 4
minimum-size-subarray-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
class Solution {
public int minimumSumSubarray(List<Integer> nums, int l, int r) {
int n = nums.size();
final int inf = Integer.MAX_VALUE;
int ans = inf;
for (int i = 0; i < n; ++i) {
int s = 0;
for (int j = i; j < n; ++j) {
s += nums.get(j);
int k = j - i + 1;
if (k >= l && k <= r && s > 0) {
ans = Math.min(ans, s);
}
}
}
return ans == inf ? -1 : ans;
}
}
// Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
func minimumSumSubarray(nums []int, l int, r int) int {
const inf int = 1 << 30
ans := inf
for i := range nums {
s := 0
for j := i; j < len(nums); j++ {
s += nums[j]
k := j - i + 1
if k >= l && k <= r && s > 0 {
ans = min(ans, s)
}
}
}
if ans == inf {
return -1
}
return ans
}
# Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
class Solution:
def minimumSumSubarray(self, nums: List[int], l: int, r: int) -> int:
n = len(nums)
ans = inf
for i in range(n):
s = 0
for j in range(i, n):
s += nums[j]
if l <= j - i + 1 <= r and s > 0:
ans = min(ans, s)
return -1 if ans == inf else ans
// Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
// class Solution {
// public int minimumSumSubarray(List<Integer> nums, int l, int r) {
// int n = nums.size();
// final int inf = Integer.MAX_VALUE;
// int ans = inf;
// for (int i = 0; i < n; ++i) {
// int s = 0;
// for (int j = i; j < n; ++j) {
// s += nums.get(j);
// int k = j - i + 1;
// if (k >= l && k <= r && s > 0) {
// ans = Math.min(ans, s);
// }
// }
// }
// return ans == inf ? -1 : ans;
// }
// }
// Accepted solution for LeetCode #3364: Minimum Positive Sum Subarray
function minimumSumSubarray(nums: number[], l: number, r: number): number {
const n = nums.length;
let ans = Infinity;
for (let i = 0; i < n; ++i) {
let s = 0;
for (let j = i; j < n; ++j) {
s += nums[j];
const k = j - i + 1;
if (k >= l && k <= r && s > 0) {
ans = Math.min(ans, s);
}
}
}
return ans == Infinity ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.