ReviseAlgo Logo
MediumVery High Frequency35 min

Sliding Window

Handle subarray/substring problems efficiently using a dynamic window over the input.

Maintain a dynamic window over a sequence to find subarrays/substrings satisfying a condition in O(n) instead of O(n²) or O(n┬│).

The Sliding Window Technique

Operations — Step by Step

Fixed Window (k=3)

O(n)Slide right: add new element, subtract removed element
Step-by-step1 / 4
marker
2
0
marker
1
1
marker
5
2
marker
1
3
marker
3
4
marker
2
5

Initial window [2,1,5], sum=8

Build the first window of size k=3. Sum = 2+1+5 = 8. This is the initial max.