Arrays

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Maintain a moving window and update its state as the boundary changes.

Step 1 / 5
Problem shape
Current lesson

Sliding Window Lab

Arrays Intermediate

Sliding Window

Welcome to the Sliding Window algorithm lab. Maintain a moving window and update its state as the boundary changes. Use it when: Use for subarray, substring, max/min length, and fixed-size window problems. The time complexity is O(n), and the space complexity is O(1) to O(k). The pseudocode is: left = 0 for right from 0 to n - 1 add a[right] to window while window is invalid remove a[left] left++ As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(1) to O(k)

Pseudocode

left = 0
for right from 0 to n - 1
  add a[right] to window
  while window is invalid
    remove a[left]
    left++
Open deeper structure lesson
1.0x