Animation9 min
The Fixed Window Slide
Constant-size windows updated in O(1) per step.
This animation runs a maximum-sum search with a translucent rectangle exactly k cells wide sitting over the array. Each step, the rectangle shifts right by one cell: the leaving cell flies out with a "−value" label, the entering cell flies in with a "+value" label, a running-sum counter ticks to its new total, and a best-so-far marker snaps onto any window that sets a record.
What to watch for:
- Only two cells ever change hands. However wide the window, each slide touches exactly one leaver and one enterer — that's the O(1)-per-step update that beats re-summing all k cells.
- Which cell leaves. When the cell at index
rightenters, the cell atright − kexits. Watch the gap between them stay exactly k — this is the off-by-one that bites in real code. - The counter never resets. The sum is carried forward and adjusted, never rebuilt. If you catch yourself mentally re-adding the whole window, the animation is showing you the cheaper habit.
By the final slide you should be able to predict the next sum before the counter ticks.