MentorNode
Start free

Pattern 02 of 08 · sliding-window

Sliding Window

Maintain an active contiguous subarray/substring window that grows and shrinks monotonically to track optimum metrics in O(N) time.

Builds on
Two Pointers
Unlocks
The end of this branch.

See the invariant

Step through it
Pattern InvariantSliding Window

Maximum Sum Subarray of Fixed Size K = 3

Max: 8
Step 1 of 4
L
2
[0]
1
[1]
R
5
[2]
1
[3]
3
[4]
2
[5]
Window Slice: [2 + 1 + 5] = 8

Initial window of size K=3 established: [2, 1, 5]. Sum = 8. Max recorded = 8.

Speed:

Practice it

3 problems
  1. 1Max Consecutive Ones IIImax-consecutive-ones-iiimedium
  2. 2Longest Substring Without Repeating Characterslongest-substring-without-repeating-charactersmedium
  3. 3Minimum Window Substringminimum-window-substringhard