Two Pointers
The Two Pointers pattern involves iterating over data structures (usually arrays or strings) using two references running at different speeds or from opposite directions.
Common Patterns
- Opposite Direction (Left & Right): Used in sorted arrays for searching pairs (e.g. Two Sum II, Container With Most Water, Trapping Rain Water).
- Same Direction (Fast & Slow): Used for removing duplicates and detecting cycles (Floyd’s Tortoise and Hare).