ƒ(x) PatternsIntermediate interactive
Two Pointers
Two indices converging or chasing through data.
two_pointers
step 1 / 6
L
2
05
18
212
315
421
526
6R
30
72 + 30 = 32 < 33 → move left pointer right.
left / right pointermatchtarget = 33
speed1×
How it works
The two-pointer pattern uses a pair of indices — moving toward each other or one chasing the other — to solve array and string problems in a single O(n) pass instead of O(n²) nested loops. Classic on sorted arrays for pair-sum and reversal.
Mental models
- On a sorted array, move the pointer that brings the sum toward target.
- Fast/slow pointers detect cycles and find midpoints.
Complexity
- Time
- O(n)
- Space
- O(1)
Reach for it when
- Pair-sum on sorted arrays
- Palindrome checks
- In-place dedup