Sliding Window

Maintain a contiguous range and update its state incrementally as the window moves.

Maintain a contiguous range and update its state incrementally as the window moves.

The window always contains the largest valid range ending at the current right boundary while satisfying the constraint.

Each element is added and removed at most once, making each step O(1) amortized.

Reusing prior window state avoids recomputing from scratch on every position.