Data Structures19 sections · 729 units
Open in Course

Fixed Window Pattern

Size k subarrays

For a fixed window of size kk:

1.1. Build the initial window with the first kk elements.

2.2. Slide the window: add the next element, remove the first element.

3.3. Update your answer at each position. Each element enters once and leaves once. Total: O(n)O(n). This pattern works for maximum sum of size kk, average of size kk, or any aggregate over fixed-size subarrays.