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.
- Valid Palindrome — check palindrome ignoring case and punctuation.
- 3Sum — find every unique triplet that sums to zero.
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.