Pattern 2 - Same Direction

Both pointers move in the same direction.

The same direction pattern has both pointers starting at the beginning (or end), moving in the same direction at different speeds.

When to use:

  • Removing elements in place.
  • Partitioning arrays.
  • Detecting cycles (fast/slow).

Subpatterns: 1.1. Fast and slow: One pointer moves twice as fast. Used for cycle detection.

2.2. Read and write: One pointer reads, one writes valid elements. Used for in-place removal.

3.3. Anchor and explorer: One pointer stays, one explores ahead.

core idea: The slow pointer tracks the "good" portion. The fast pointer scans ahead.