Minimum Size Subarray Sum

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Minimum Size Subarray Sum

Approach

Use a variable-size window. Expand right to include more elements and grow the running sum. Whenever the sum meets or exceeds the target, try shrinking from the left to find the smallest valid window, updating the minimum length each time.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Sliding Window (Variable Size)

Why It Works

Each element is added once (right pointer) and removed at most once (left pointer), so the total work is O(2n) = O(n). The inner while loop shrinks greedily, ensuring we find the smallest window that satisfies the sum condition.

Updated Feb 2026