What Is a Variable Window? Growing and Shrinking a Subarray

A variable window grows on the right and shrinks on the left. With zeros and positive numbers, the left edge only moves forward, so the scan stays linear.

A fixed window always holds k elements. A variable window changes length. The right edge advances one step at a time. The left edge moves forward only while the slice is too large for the question.

How it works

Keep a running sum. Add nums[right]. While the sum breaks the rule, subtract nums[left] and increment left. Because the numbers are zero or positive, shrinking never needs to be undone. Each index enters once and leaves once, so the whole scan is O(n).

The shortest slice whose sum is at least a target records a length whenever the sum qualifies, then shrinks to see if a shorter slice still qualifies. The longest slice whose sum is at most a target shrinks until the sum fits, then records that length. If nothing qualifies, the answer stays 0.

A negative number can make a longer slice smaller, and the "shrink until it fits" story stops being safe. These exercises leave negatives out on purpose.

Theme
Terminal

Press Run to execute this example. Use console.log to print a value.

Expected output: 2, then 0. In the first call, the window [4, 3] is the shortest qualifying slice.

Worked example

At right index 4, the window [3, 1, 2, 4] totals 10. Repeated shrinking records lengths 4 and 3 before the sum falls below 7. Adding the final 3 creates [2, 4, 3]; shrinking once more discovers [4, 3] with length 2.

Why it is correct

Before each right-edge advance, every window ending earlier has already been considered. Once the sum reaches the target, the current window qualifies. Removing values from the left tests every shorter window with the same right edge until the sum no longer qualifies. Non-negative values guarantee that further shrinking cannot restore a too-small sum, so no qualifying candidate is skipped. The smallest recorded length is therefore optimal.

Cost

The right pointer advances n times. The left pointer also advances at most n times across the entire run, even though it sits inside a nested while loop. Total time is O(n), and the scalar state uses O(1) extra space. With negative values, this monotonic argument fails.

In the course

The sliding-window lesson keeps k fixed so the update is obvious. Grow, then shrink is the next lesson. A prefix sum can answer a range too. You do not need one while the window already holds the sum. rangeSum is the case that does: with prefix[0] === 0, the inclusive span from left through right is prefix[right + 1] - prefix[left], in O(1) after the prefix exists.

Common mistakes

Moving the left pointer backward revisits values and can make the scan quadratic.

Recording a length before the sum qualifies reports a slice the tests reject.

Using this shrink rule on negatives. A later, smaller sum might need a wider window you already discarded.

When to use it

Use a variable window for a contiguous slice whose length is part of the answer, when the values only move the sum in one direction.

When not to

Do not use it when the slice does not have to be contiguous. A subset of any indexes is a different search.

Problem-solving checklist

Write the window invariant, including whether it is valid before or after shrinking. Confirm that every value is non-negative before relying on monotonic sums. Record the answer at the exact moment the invariant says it is valid. Test no solution, one-element success, and success at each end.

Optional enrichment

Write the longest-window variant for a sum at most a positive limit. State exactly when the window is valid, and test zeros because they can extend a valid window without changing its sum.