Two Pointers — definition.
Definition
Two cursors moving independently over a sorted or monotone structure.
Complexity
Time O(n) after an optional O(n log n) sort; space O(1).
When to use it
- Input is sorted or can be sorted.
- Answer involves a pair, triple, or partition with a monotone predicate.
- You want to collapse O(n²) brute force to O(n).
Representative scenarios
- Scale Balancer — find a pair summing to target.
- License Plate Mirror — check palindrome under constraints.
- Triple Alloy — find a triplet meeting a condition.
Related patterns
Read the full pattern page: Two Pointers — template, code, and drills.
Run the free diagnostic.
Ten-minute patterns quiz. No card. Personalized loop starts on the other side.