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

  1. Opposite Direction (Left & Right): Used in sorted arrays for searching pairs (e.g. Two Sum II, Container With Most Water, Trapping Rain Water).
  2. Same Direction (Fast & Slow): Used for removing duplicates and detecting cycles (Floyd’s Tortoise and Hare).

Solved Problems