Minimum Size Subarray Sum
Find the shortest subarray whose sum reaches a target, growing and shrinking a window of positive numbers.
The problem
You get an array nums of positive integers and a positive integer target. Return the length of the shortest contiguous subarray whose sum is at least target, or 0 if no subarray reaches it.
Input target = 9, nums = [2, 5, 1, 4, 6, 1]
Output 2
4 + 6 = 10 reaches 9 with two numbers, and no single number does.
Input target = 20, nums = [3, 4, 5]
Output 0
Even all three numbers add up to only 12.
Key constraint: Every number is positive, so growing a window always raises its sum and shrinking it always lowers the sum.
How to spot the pattern
- You want the shortest contiguous subarray that meets a sum condition.
- All numbers are positive, so the window's sum moves in a predictable direction as it grows or shrinks.
The idea
Grow a window to the right until its sum reaches target. Then shrink it from the left for as long as the sum still reaches target, recording the length each time, because a shorter valid window is exactly what you want. Since every number is positive, a window that drops below target can only be fixed by growing again, so both edges only ever move forward.
Brute force: Start at every index and extend until the sum reaches the target, which takes O(n^2) time.
Step by step
- 1Set
left = 0,sum = 0, andbestto infinity. - 2For each
right, addnums[right]tosum. - 3While
sum >= target, updatebestwithright - left + 1, subtractnums[left]fromsum, and moveleftright. - 4Return
best, or 0 if it never changed.
See the pattern move
The sliding window pattern on a small example. Step through it, or press play.
Code
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
left = 0
window_sum = 0
best = float("inf")
for right, num in enumerate(nums):
window_sum += num
while window_sum >= target: # shrink while the window still works
best = min(best, right - left + 1)
window_sum -= nums[left]
left += 1
return 0 if best == float("inf") else bestComplexity
Time
O(n)
Space
O(1)
Each number enters the window once and leaves at most once, so the inner loop adds only O(n) work in total.
Watch out for
- Shrinking with
ifinstead ofwhile: in [1, 1, 1, 1, 5] with target 5, the window must drop four numbers in a row to reach the answer, 1. - Returning infinity or the largest int when no window works; the problem wants 0.
- Using this on arrays with negative numbers, where shrinking the window can raise its sum.
Follow-up questions
Can you solve it in O(n log n) with binary search?
Yes. Because every number is positive, the prefix sums are increasing, so for each start i you can binary search the first end j with prefix[j] - prefix[i] >= target.
What if the numbers can be negative?
The window argument breaks. Use prefix sums with a deque of candidate start positions kept in increasing order of prefix sum; that is Shortest Subarray with Sum at Least K.
In Hinglish
Sum target tak pahunchne tak right badhao, phir jitna ho sake left se chhota karo, aur har valid window ki length note karo.