const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Minimum Size Subarray Sum: LeetCode 209 Walkthrough

Minimum Size Subarray Sum (#209) is the purest variable-size sliding window problem on positive integers — expand right until sum >= target, then shrink left to minimize the window length.

7 min read|

Variable-size sliding window in its purest form

Expand right, shrink left, minimize length — O(n) every time

Minimum Size Subarray Sum: The Purest Sliding Window

Minimum Size Subarray Sum (#209) is one of the cleanest introductions to the variable-size sliding window pattern on LeetCode. Unlike fixed-window problems where you know the window size in advance, this problem asks you to find the smallest contiguous subarray whose sum meets or exceeds a target value. That single twist — variable width — is what makes it the foundation for harder window problems.

If you have solved fixed-size sliding window problems and are ready to level up, this is where you start. The leetcode 209 solution teaches you how to expand and contract a window dynamically, a skill that transfers directly to problems like Minimum Window Substring (#76) and Longest Substring Without Repeating Characters (#3).

In this walkthrough, we will break down the problem, build the sliding window approach step by step, trace through a visual example, and cover the edge cases that trip up many candidates in interviews.

Understanding the Problem

You are given an array of positive integers nums and a positive integer target. Find the minimum length subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.

For example, given nums = [2, 3, 1, 2, 4, 3] and target = 7, the answer is 2 because the subarray [4, 3] sums to 7 and no single element reaches the target alone. The smallest subarray sum that meets the threshold has length 2.

The key constraint that makes this problem tractable is that all elements are positive. This guarantees a critical property: adding an element to the window always increases the sum, and removing one always decreases it. Without this monotonic behavior, the sliding window technique would not apply directly.

Sliding Window Approach for Minimum Size Subarray Sum

The brute-force solution checks every possible subarray, which takes O(n^2) time. The sliding window minimum approach cuts this down to O(n) by maintaining two pointers — left and right — that define the current window.

The algorithm works in two phases that alternate. First, expand: move right forward and add nums[right] to the running sum. Second, shrink: while the sum is greater than or equal to the target, record the current window length as a candidate answer, subtract nums[left] from the sum, and advance left. This shrinking step is where the variable window subarray technique earns its efficiency.

Why does this work? Because every element is positive, once the window sum meets the target, the only way to find a shorter valid window is to remove elements from the left. And once the sum drops below the target, you must expand right again. Each element enters the window exactly once and leaves exactly once, giving you O(n) total operations.

💡

Pro Tip

The inner while loop shrinks from the left as long as sum >= target — this is the variable-window technique. Each element enters and leaves the window at most once, so it's O(n) total.

Implementation — Minimum Length Subarray

The implementation is remarkably concise once you understand the two-phase expand-shrink loop. Here is the step-by-step approach in plain English, which translates directly to code in any language.

Initialize left = 0, sum = 0, and answer = Infinity (or the array length + 1, which signals "no valid window found"). Then iterate right from 0 to the end of the array. For each position of right, add nums[right] to sum. While sum >= target, update answer to the minimum of answer and (right - left + 1), subtract nums[left] from sum, and increment left. After the loop, return answer if it was updated, otherwise return 0.

  1. 1Set left = 0, sum = 0, answer = Infinity
  2. 2For each right from 0 to nums.length - 1: add nums[right] to sum
  3. 3While sum >= target: answer = min(answer, right - left + 1), then sum -= nums[left] and left++
  4. 4After the loop: return answer === Infinity ? 0 : answer

Visual Walkthrough — nums = [2,3,1,2,4,3], target = 7

Let us trace the algorithm on the classic example. We start with left = 0, sum = 0, and answer = Infinity.

Right moves to index 0: sum = 2. Not >= 7, so keep expanding. Right moves to 1: sum = 5. Still under 7. Right moves to 2: sum = 6. Still under. Right moves to 3: sum = 8. Now sum >= 7. Update answer = min(Infinity, 3 - 0 + 1) = 4. Shrink: subtract nums[0] = 2, sum = 6, left = 1. Sum < 7, stop shrinking.

Right moves to 4: sum = 6 + 4 = 10. Sum >= 7. Update answer = min(4, 4 - 1 + 1) = 4. Shrink: subtract nums[1] = 3, sum = 7, left = 2. Still >= 7. Update answer = min(4, 4 - 2 + 1) = 3. Shrink: subtract nums[2] = 1, sum = 6, left = 3. Sum < 7, stop.

Right moves to 5: sum = 6 + 3 = 9. Sum >= 7. Update answer = min(3, 5 - 3 + 1) = 3. Shrink: subtract nums[3] = 2, sum = 7, left = 4. Still >= 7. Update answer = min(3, 5 - 4 + 1) = 2. Shrink: subtract nums[4] = 4, sum = 3, left = 5. Sum < 7, stop. Final answer = 2, which corresponds to the subarray [4, 3].

⚠️

Important Caveat

This only works for POSITIVE integers — with negative numbers, shrinking the window can increase the sum. For mixed arrays, use prefix sum + binary search instead.

Edge Cases to Watch For

The most common edge case is when the target is larger than the total sum of the array. In this scenario, no subarray can satisfy the condition, and you return 0. Your initialization of answer to Infinity (or length + 1) handles this naturally — if answer was never updated, no valid window was found.

Another edge case is when a single element is greater than or equal to the target. The algorithm handles this correctly because when right lands on that element, the inner while loop will shrink the window all the way down to length 1 and record it as the answer.

A third edge case worth considering is when all elements are equal. If every element is k and the target is t, the minimum length subarray is ceil(t / k). The sliding window discovers this naturally as it expands just enough and then shrinks to the tightest valid window.

  • Target > total sum: return 0 (no valid subarray exists)
  • Single element >= target: answer is 1
  • All elements equal: answer is ceil(target / element_value)
  • Target equals exact sum of entire array: answer is the full array length

What Minimum Size Subarray Sum Teaches You

LeetCode 209 is the gateway to the variable-size sliding window family. The expand-then-shrink pattern you learn here is the exact same skeleton used in harder problems. Minimum Window Substring (#76) adds a frequency map constraint. Longest Repeating Character Replacement (#424) flips the optimization from minimize to maximize. But the core loop — expand right, shrink left while valid — is identical.

The subarray sum target constraint also teaches you to recognize when sliding window applies versus when it does not. The technique works because all values are positive, which guarantees monotonic sum behavior. When you encounter a problem with negative numbers or non-monotonic conditions, you should immediately think of alternative approaches like prefix sums or deques.

Practice this problem until the expand-shrink loop is automatic. Then move on to the harder variants. YeetCode flashcards can help you drill the pattern recognition — knowing when to apply variable-size sliding window is just as important as knowing how to implement it.

ℹ️

Pattern Context

Minimum Size Subarray Sum (#209) is the simplest variable-size sliding window on positive integers — it's the stepping stone to Minimum Window Substring (#76) and Longest Repeating Character Replacement (#424).

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now