The 30 second answer
I need the shortest subarray whose sum is at least the target. Because all values are positive, growing the window increases the sum and shrinking decreases it, so a two-pointer window works. I add on the right, then shrink from the left while the sum stays large enough, and I keep the shortest length I see. If no window qualifies I return 0.
Problem Given a target and an array of positive integers, return the minimal length of a contiguous subarray whose sum is at least the target, or 0 if no such subarray exists. Original: LeetCode 209: Minimum Size Subarray Sum.
Understanding the prompt The values are positive, which is essential: the window sum is monotonic as the window grows or shrinks. You want the shortest length, not the subarray itself. A single element that already meets the target wins with length 1.
Pattern recognition A variable-size window that shrinks to the shortest valid prefix of the current right edge. The same numbers with negative values would need prefix sums and a different structure.
Brute force Compute every subarray sum: O(n²).
Optimal Add nums[hi] into a running sum. While the sum is at least the target, record the length hi − lo + 1, subtract nums[lo], and advance lo. If you never record a length, return 0.
Alternatives Prefix sums plus a binary search for the earliest prefix that reaches prefix[r] - target is O(n log n), useful if you are asked not to use two pointers. It needs the positivity so the prefixes are increasing.
Java solution
public class MinimumSizeSubarraySum {
static int minSubArrayLen(int target, int[] nums) {
int lo = 0, sum = 0, best = Integer.MAX_VALUE;
for (int hi = 0; hi < nums.length; hi++) {
sum += nums[hi];
while (sum >= target) {
best = Math.min(best, hi - lo + 1);
sum -= nums[lo++];
}
}
return best == Integer.MAX_VALUE ? 0 : best;
}
public static void main(String[] args) {
System.out.println(minSubArrayLen(7, new int[] {2, 3, 1, 2, 4, 3})); // 2
System.out.println(minSubArrayLen(4, new int[] {1, 4, 4})); // 1
System.out.println(minSubArrayLen(11, new int[] {1, 1, 1, 1, 1, 1, 1, 1})); // 0
}
}Complexity O(n) time, O(1) extra space. Each index enters and leaves the window at most once.
Edge cases
- A single element already equals the target, as in [1,4,4] with target 4, returns 1.
- No window works, as in eight 1s with target 11, returns 0.
- The official sample [2,3,1,2,4,3] with target 7 returns 2, from the subarray [4,3].
Follow-ups
- Subarray Sum Equals K allows negatives and needs a prefix-sum hash map, not this window.
- At most target, or exactly target with positives only: small changes to the shrink condition.
Tests target 7 and [2,3,1,2,4,3] returns 2. target 4 and [1,4,4] returns 1. target 11 and [1,1,1,1,1,1,1,1] returns 0.
How to say it in the interview "All numbers are positive, so I can grow a running sum on the right and shrink from the left while the sum is still at least the target, keeping the shortest length. If I never reach the target I return 0. Linear time."
Sources (1)
- LeetCode 209: Minimum Size Subarray Sumleetcode.com