Arrays & Strings

Fixed-size and variable-size sliding window patterns

Arrays: Sliding Window

Active InputWindow size k = 3
Array
2
0
1
1
5
2
1
3
3
4
2
5
Choose a pattern to animate the window one step at a time.

Two Sliding Window Patterns

1. Fixed-size Window keeps exactly k elements in view. After the first full window is built, each step removes one value from the left and adds one from the right.

2. Variable-size Window expands until a condition becomes true, then shrinks to find a tighter valid answer. This pattern is common for minimum-length or longest-valid-subarray problems.

Complexity

Fixed Window
Time:O(n)
Space:O(1)
Variable Window
Time:O(n)
Space:O(1)