How it works
Use two indexes that move through the same sequence. They may start at opposite ends, move in the same direction at different speeds, or represent a window boundary. The useful question is: what information can one pointer make irrelevant?
Read the two pointers guide ->Use this sequence
- Define what each pointer represents.
- Move the pointer that can improve the current state.
- Prove why the discarded range cannot contain a better answer.
Complexity
Usually O(n) time and O(1) extra space.
Watch for
Moving both pointers every time, or using it when the input is not ordered and no invariant exists.