Minimum Size Subarray Sum

medium

Find the minimal length subarray with sum greater than or equal to target

Minimum Size Subarray Sum

Key Insight

Expand right to grow sum. When sum >= target, shrink left to find minimum length

Step 1Start Expanding
Target: 7Sum: 2
L
R
2
0
3
1
1
2
2
3
4
4
3
5
[———]window

Begin with a single element. Sum is less than target, keep expanding.

1 / 6
Practice the Code

Step-by-Step Walkthrough: Minimum Size Subarray Sum

Expand right to grow sum. When sum >= target, shrink left to find minimum length

  1. Start Expanding

    Begin with a single element. Sum is less than target, keep expanding.

  2. Keep Expanding

    Expand until the window sum meets or exceeds the target.

  3. Shrink Left

    Remove left element to try a shorter window.

  4. Expand Again

    Sum dropped below target. Expand right to include more elements.

  5. Shrink to Minimum

    Keep shrinking left to find the smallest valid window.

  6. Found Minimum

    Window [4, 3] sums to 7 with length 2. This is the minimum.