Two Pointers
Use two pointers moving through the data to find pairs, partitions, or solve problems on sorted arrays in O(n) time.
- • Sorted array/string
- • Finding pairs that sum to target
- • Removing duplicates in-place
- • Partitioning elements
- • Unsorted data without sorting first
- • Need to track more than 2 positions
- • Forgetting to handle equal elements
- • Pointer collision conditions
- • Not sorting first when required
Key Invariant
Pointers narrow the search space by eliminating impossible candidates each step