Minimum Size Subarray Sum
A medium Sliding Window problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Sliding Window
- Sheets
- 2
- Core for
- 5 roles
- Platform
- LeetCode
The problem
Given an array of positive integers and a target sum, find the minimal length of a contiguous subarray whose sum is at least the target.
Example 1
- Input
- target = 7, nums = [2,3,1,2,4,3]
- Output
- 2
- Why
- The subarray [4,3] has sum 7 with length 2, which is the minimal.
Example 2
- Input
- target = 4, nums = [1,4,4]
- Output
- 1
- Why
- The single element [4] has sum 4.
Constraints
- 1 <= target <= 10^9
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^5
How to think about it
Updated 2026-09-09Every element is strictly positive, so extending the right endpoint can only increase the sum and advancing the left endpoint can only decrease it. Once a running span meets the target threshold, stretching it further to the right cannot yield a shorter valid window, meaning the left boundary must immediately contract to seek smaller lengths.
Approaches, worst first
Check all pairs
time O(n^2) · space O(1)
Evaluate the sum of every possible subarray from each starting index. Redundant because expanding an already qualified span wastes time exploring longer candidates.
Prefix sums with binary search
time O(n log n) · space O(n)
Build an array of cumulative sums. For each starting point, binary search for the first end index that reaches prefix[i] + target. Monotonicity holds because terms are positive, but the search takes extra time.
Shrinkable sliding windowWrite this one
time O(n) · space O(1)
Expand the right pointer while accumulating values into a running total. Whenever the sum reaches the target, record the current length and shrink from the left until the sum drops back below target.
Where people lose marks · 3
- Returning n + 1 or the sentinel initial value when no subarray ever reaches target instead of returning 0.
- Using an if statement instead of a while loop when shrinking the left edge skips further contractions that still satisfy the target.
- This two-pointer contraction relies strictly on all elements being positive; if zero or negative values were allowed, shrinking could increase or preserve the sum.
The theory behind it
Sliding Window — the ground this problem stands on. All Sliding Window problems
What Sliding Window is
A sliding window is an adjustable magnifying lens placed over a continuous segment of a sequence. Rather than recalculating metrics for every potential subsection from scratch, the window expands rightward by absorbing fresh elements and contracts leftward to expel stale entries. Only data currently framed within the window borders contributes to the active calculation.
When to reach for it
Reach for a sliding window when a question asks for the longest, shortest, or optimal contiguous subarray or substring matching a constraint. Key signals include fixed window sizes like maximum sum across k consecutive values, or dynamic criteria like finding the shortest substring holding all target characters. If the target subset must form an unbroken continuous run, window mechanics replace repetitive segment rescanning.
How the pattern works
Maintain two boundary indices, left and right, defining the active interval alongside a running state accumulator. In each step, expand the right boundary to incorporate the incoming element into state totals. When current state violates the designated problem constraints, increment the left boundary while deducting departing values until validity is restored. Update your tracking metric, whether minimum window length or maximum score, only during valid intervals.
What each operation costs
| Operation | Time |
|---|---|
| slide window across full array length | O(n) |
| update running aggregate per incoming element | O(1) |
| auxiliary window frequency map storage | O(k) |
What usually goes wrong with Sliding Window
- Shrinking the left border using an if statement instead of a while loop, allowing invalid window conditions to persist across iterations.
- Updating optimum results before validating window legality, recording illegal states that contain duplicate items or violate length requirements.
- Forgetting to decrement left element frequencies or remove empty keys from tracking maps when advancing the left boundary forward.
Which roles need this problem
Sliding Window is a core topic for these 5 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 10 more roles, including SDE / Backend Engineer, Data Engineer, ML Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Sliding Window problems
Problem set and role mapping as of .