Same Direction / Expanding Window
Core idea: Both pointers travel left โ right, never backward. A fast (read/scan) pointer explores ahead while a slow (write/anchor) pointer trails, marking a boundary you maintain โ the end of the compacted region, or the start of the current run. Because each pointer crosses the array at most once, the work is O(n).
The shape of the pattern
The two same-direction pointers move at different speeds, and the gap between them encodes something useful: how many elements you've kept, how long the current window is, or where a run began. This is what lets you avoid the O(nยฒ) "for each element, re-scan the rest" loop.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson