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, Python, C++ and JavaScript, 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)
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
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 code in Java, Python, C++ and JavaScript.
| 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;
}
}class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
best = float('inf')
for i in range(len(nums)):
total = 0
for j in range(i, len(nums)):
total += nums[j]
if total >= target:
best = min(best, j - i + 1)
break
return 0 if best == float('inf') else bestclass Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int best = INT_MAX;
for (int i = 0; i < (int) nums.size(); i++) {
int sum = 0;
for (int j = i; j < (int) nums.size(); j++) {
sum += nums[j];
if (sum >= target) { best = min(best, j - i + 1); break; }
}
}
return best == INT_MAX ? 0 : best;
}
};function minSubArrayLen(target, nums) {
let best = Infinity;
for (let i = 0; i < nums.length; i++) {
let sum = 0;
for (let j = i; j < nums.length; j++) {
sum += nums[j];
if (sum >= target) { best = Math.min(best, j - i + 1); break; }
}
}
return best === Infinity ? 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;
}
}class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
l, total, best = 0, 0, float('inf')
for r in range(len(nums)):
total += nums[r]
while total >= target:
best = min(best, r - l + 1)
total -= nums[l]
l += 1
return 0 if best == float('inf') else bestclass Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int l = 0, sum = 0, best = INT_MAX;
for (int r = 0; r < (int) nums.size(); r++) {
sum += nums[r];
while (sum >= target) {
best = min(best, r - l + 1);
sum -= nums[l++];
}
}
return best == INT_MAX ? 0 : best;
}
};function minSubArrayLen(target, nums) {
let l = 0, sum = 0, best = Infinity;
for (let r = 0; r < nums.length; r++) {
sum += nums[r];
while (sum >= target) {
best = Math.min(best, r - l + 1);
sum -= nums[l++];
}
}
return best === Infinity ? 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, Longest Repeating Character Replacement, Longest Substring Without Repeating Characters.
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.