Compile Ready
Module 1 · Sliding Window Fundamentals

Shrinking the Window

Shrinking the window advances left to restore validity, remove unnecessary elements, or minimize a valid range.

8 min readConcept
Sliding WindowShrinkingLeft PointerInvariants

Left Pointer Restores the Contract

The left pointer is the repair pointer. When expansion breaks the invariant, left moves forward and the algorithm removes elements from the maintained state until the invariant is true again. Each removal must undo exactly what the earlier expansion did for that element.

This repair loop is what makes a variable window safe. You can be aggressive about expanding because you know every invalid state has a local repair operation: advance left, update the state, and recheck the invariant.

Shrink While Invalid vs Shrink While Useful

For longest-valid problems, shrink while the window is invalid. Once the loop ends, the window is valid and as far left as it can be under that right endpoint, so its length is a candidate. This is the pattern for longest subarray with sum at most target or longest substring with at most k distinct characters.

For shortest-valid problems, shrink while the window is already valid enough to answer. Record the current length, remove the left element, and see whether the window still satisfies the requirement. This is the pattern for minimum size subarray sum and minimum window substring.

Minimization and Tight Windows

A tight window is one where removing the leftmost element would break the property you care about. In minimization problems, tightness is the goal because every extra element makes the window longer than necessary. In longest-valid problems, tightness after repair prevents a hidden invalid prefix from polluting the answer.

Do not confuse tightness with fixed size. A variable window may have many different tight sizes depending on the right endpoint. The invariant decides how far left can move, not a predetermined length.

Off-by-One Discipline

When left moves, compute any answer that needs the old window before removing the left element. For a shortest valid window, record right - left + 1 first, then subtract the leaving value and increment left. For a longest valid window, repair first, then record length.

Boundary mistakes usually come from mixing those two timings. Ask whether the current window should be counted before or after the left element leaves. The answer follows from the invariant and from whether you are maximizing valid windows or minimizing sufficient ones.

Two reasons to shrink

Loading…

The first method shrinks to restore validity. The second shrinks while the window is useful so it can find the shortest sufficient range.

Key Takeaways

  • Shrinking removes the leftmost element and advances **left** while updating window state.
  • Longest-valid problems usually shrink while invalid, then update the best length.
  • Shortest-valid problems usually update first, then shrink while the requirement still holds.
  • The timing of answer updates depends on whether the current window should be counted before or after removal.