A report needs the largest total among all 30-day spans in a series of 100,000 readings. A sliding window keeps a running total between two boundaries. Moving right adds one reading. Moving left subtracts one. Each new span costs two arithmetic operations instead of another 30-value scan.

Sorting would destroy the ranges being measured. Sliding windows preserve the input order because every candidate must remain contiguous.

Visualization

The trace finds the longest substring without repeating characters in abcabcbb. The right boundary admits a, b, and c, so the current window and best substring both become abc. When the next a enters at index 3, the trace names the duplicate and marks it red before the left boundary advances past the previous a; the accepted window becomes bca, while the best remains abc with length 3.

Every index enters the window once and leaves at most once. A last-seen index identifies whether the entering character already exists inside the current window and moves the left boundary to one position past that duplicate; neither boundary moves backward.

This accounting holds when entry and exit update validity incrementally. A running sum, element count, or frequency map qualifies because one value changes at a boundary. A window maximum needs an additional structure because removing the maximum does not reveal the next-largest retained value.

Complexity
n
number of elements in the input sequence
k
window width
σ
number of distinct symbols the input alphabet can contain

Where the Pattern Breaks

A fixed-size window contracts on a schedule. Every addition at the right removes one value at the left, so the width stays k. A variable-size window contracts only when its constraint says to. One right step may trigger several left steps or none. Using the fixed rule on a variable problem prevents the width from adapting. Using the variable rule without a stopping condition shrinks past the useful range.

Removal must update the aggregate from the old state and the leaving value. A running sum or per-key frequency count can do that directly. A scalar maximum cannot. When its current maximum leaves, finding the next one requires information about the rest of the window.

Negative values break the usual contraction proof. Extending a window can lower its sum, so a range that currently fails may become valid after another value arrives. The sum remains cheap to update, but it no longer tells the left boundary when an entire family of ranges can be discarded. Exact-target problems with negatives can use prefix sums plus a hash map instead. That combination finds a target range without assuming that sum grows with length.

Diagram and C# Implementation

The diagram shows a minimum-satisfying window: record a candidate when the constraint holds, then keep shrinking while it remains valid. A maximum-valid window reverses that control order. It shrinks while invalid, then records the candidate after validity has been restored.

Comparison

TechniqueRequired inputStronger caseWeaker case
Sliding windowContiguous range. Aggregate reversible on removalContiguous sub-array or substring with an incrementally maintained aggregateNon-reversible extreme, or a constraint non-monotone in length
Brute-force recomputeNoneTiny inputs or a one-off computationRepeats work across overlapping windows
Two PointersOften sorted. Two ends converging inwardFinding a pair/partition by moving opposite ends toward each otherA single window with a running aggregate over its interior
Prefix SumPrecomputed cumulative arrayMany static range-sum queries. Sums involving negativesOne constraint-driven window that must also report its members
Monotonic Stack and QueueDeque of candidate indicesMaximum/minimum of every windowPlain reversible aggregates where a scalar already suffices

If the two ends converge rather than trail in the same direction, the shape is Two Pointers. For a maximum or minimum that a scalar cannot reverse on removal, a monotonic deque keeps only the candidates that may become the next extreme.

References