Minimum Size Subarray Sum
Given an array of positive integers and a target, find the minimum length subarray whose sum is greater than or equal to the target. Return 0 if no such subarray exists.
This is a premium problem.
Unlock Minimum Size Subarray Sum with both full courses, DSA and System Design, and every other premium problem.
Problem
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
Input
A positive integer `target` and an array of positive integers `nums`.
Output
The minimum length of a subarray with sum >= target, or 0 if no such subarray exists.
Examples
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
[4,3] has sum 7 and length 2. No subarray of length 1 meets the target.
Input: target = 4, nums = [1,4,4]
Output: 1
The single element [4] already meets the target.
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
The total sum of the array (8) is less than 11. No valid subarray exists.
The brute-force approach
Try every possible subarray. For each starting index, extend the subarray one element at a time until the sum reaches the target. Track the shortest such subarray found.
min_len = float('inf')
for i in range(len(nums)):
total = 0
for j in range(i, len(nums)): # extend right
total += nums[j]
if total >= target:
min_len = min(min_len, j - i + 1)
break # no need to go longer from this start
return 0 if min_len == float('inf') else min_lenYou're recomputing sums starting from every index, which is O(n²). The key insight: all values are positive, so once a window's sum is large enough, shrinking it from the left is the only way to find something shorter. A sliding window handles that in one pass.
Spotting the pattern
This is a Sliding Window problem. The key question to ask yourself:
Once my running sum hits the target, how do I know if I can shrink the window and still meet the condition?
Answering that is where it clicks, and it's exactly what the guided walkthrough below builds with you: the pattern reasoning, a progressive hint ladder that never spoils the answer, a row-by-row dry run, the optimized solution, and an in-browser editor to run your code against real test cases.
Unlock the full guided solution
The Sliding Window walkthrough for Minimum Size Subarray Sum: the progressive hint ladder, a row-by-row dry run, the optimized code, and an in-browser runner, plus both full courses, DSA and System Design, and every other premium problem.
Already finished Big-O and Hash Maps free? This picks up right where the path leads: same format, harder patterns, the ones that show up in every FAANG-style screen.
What you get
- ✓The full DSA course: concept lessons, drills, graded tests, and cold-read capstones for every pattern
- ✓The full System Design course: 20 building-block units for the design round, included at no extra cost
- ✓Stop going blank on algorithm problems. The hint ladder walks you to the answer without spoiling it.
- ✓Actually internalize the trace: dry runs walk state row by row so it clicks before you write a line
- ✓Know when to reach for each pattern. Every problem names the trigger so you spot it next time.
- ✓Write and run JS or Python in the browser with real test cases on every problem
- ✓Access every new problem, unit, and pattern guide as they ship weekly
“This app finally made it click. I tried NeetCode, CTCI, and YouTube for years and still couldn't solve Two Sum. The beginner mental models for the patterns are genuinely so helpful. THANK YOU.”
Full access
$49.99 / year
or $9.99 / month
Now includes a C++ runner alongside JavaScript and Python.
LeetCode Premium is $159/year and doesn't teach you anything.
Less than one Udemy course, with guided problems instead of passive lectures.
One extra month of job searching costs more than a full year here.
- ✓Both courses, every unit: DSA + System Design
- ✓All 150 problems across 33 patterns, plus every new one
- ✓New problems and units weekly
- ✓Cancel anytime
Cancel anytime. Not useful within 7 days? Email for a full refund. Secured by Stripe.