Minimum Size Subarray Sum
Minimum Size Subarray Sum is a medium arrays & hashing problem solved with the sliding window · variable pattern.
The best approach, optimal (variable sliding window), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, from brute force up.
Problem
Given an array of positive integers nums and a positive integer target, return the length of the shortest contiguous subarray whose sum is at least target. If none exists, return 0.
Examples
Example 1
- Input
target = 8, nums = [3, 1, 2, 5, 2, 4]- Output
3- Why
- [1, 2, 5] sums to 8 with length 3. No pair of neighbours reaches 8 (the largest is 5 + 2 = 7).
Example 2
- Input
target = 20, nums = [2, 3, 4]- Output
0- Why
- Even the whole array sums to only 9.
Constraints
1 <= target <= 10^91 <= nums.length <= 10^51 <= nums[i] <= 10^4(all positive)
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Optimal (variable sliding window) | O(n) | O(1) |
1Brute force
O(n²)O(1)For each start, extend until the sum reaches target and record the length.
- For each i, add nums[j] for j >= i until sum >= target.
- Track the smallest j - i + 1.
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int best = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = i; j < nums.length; j++) {
sum += nums[j];
if (sum >= target) { best = Math.min(best, j - i + 1); break; }
}
}
return best == Integer.MAX_VALUE ? 0 : best;
}
}2Optimal (variable sliding window)
O(n)l and r each move at most n times.O(1)Grow the window on the right. While its sum is at least target, record the length and shrink from the left. Because values are positive, each element enters and leaves the window once.
- For each r: sum += nums[r].
- While sum >= target: best = min(best, r - l + 1); sum -= nums[l++].
- Return best, or 0 if it never changed.
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int l = 0, sum = 0, best = Integer.MAX_VALUE;
for (int r = 0; r < nums.length; r++) {
sum += nums[r];
while (sum >= target) {
best = Math.min(best, r - l + 1);
sum -= nums[l++];
}
}
return best == Integer.MAX_VALUE ? 0 : best;
}
}Edge cases to test
- No subarray reaches the target (return 0)
- A single element already reaches the target
Hints
Hint 1
All numbers are positive, so growing the window only increases the sum and shrinking only decreases it.
FAQ
What is the best time complexity for Minimum Size Subarray Sum?
Optimal (variable sliding window) runs in O(n) time and O(1) extra space. l and r each move at most n times.
Which pattern does Minimum Size Subarray Sum use?
It is a arrays & hashing problem that uses the sliding window · variable pattern. Other problems with the same pattern: Count of Subarrays with Product Less Than K.
Is there a brute force solution for Minimum Size Subarray Sum?
Yes. Brute force takes O(n²) time and O(1) space. For each start, extend until the sum reaches target and record the length.
Which edge cases should I test for Minimum Size Subarray Sum?
No subarray reaches the target (return 0); A single element already reaches the target.