When Sliding Window Applies

Recognizing sliding window problems.

Look for these signals:

1.1. Contiguous sequence: The problem asks about subarrays or substrings, not subsequences.

2.2. Window optimization: Find longest, shortest, or optimal value over a contiguous range.

3.3. Monotonic validity: Adding elements only makes the window valid or invalid in one direction. Removing from the other end can restore validity.

4.4. Original order: You process elements as they appear. No sorting or rearranging.

If shrinking the window can't restore a broken constraint, sliding window won't work. Consider DP instead.