Two Pointers
Tactic
Two pointers put two positions under deliberate control. One pointer can start at each end, both can move forward at different speeds, or one can read while the other writes a compacted result.
The invariant explains why moving one pointer discards work safely. In a sorted pair search, a sum that is too small proves the left value cannot pair with the current right value. In a palindrome check, matching endpoints prove the unresolved work is inside the boundary.
Use the pointer movement as the proof. Before writing code, state what region has already been eliminated and what region still contains every possible answer. If neither pointer movement eliminates a region, the tactic is only guesswork.
Value
The value is search-space pruning without extra structure. Two pointers often turns a pair or boundary question from a quadratic enumeration into a linear walk, and it does that while keeping memory constant.
Direct complexity example
- Brute force: Try every pair in a sorted array: time and space.
- With this tactic: Move
leftorrightbased on the invariant: time and space. - Space: Sorting first costs time and may cost space depending on the language sort. If the input is already sorted, the pointer phase is the whole cost.
Challenges this solves
- sorted pair search
- palindrome and symmetry checks
- in-place compaction
- container or boundary optimization
- n-sum after sorting
When to use it
Use this tactic when these conditions are true:
- the input is sorted or can be sorted without losing required order
- a low or high value tells you which side cannot work
- the task compares positions rather than building all combinations
- the answer can be maintained while boundaries move inward or forward
When not to use it
Reach for a different tactic when these warning signs appear:
- the array order is meaningful and sorting would change the answer
- moving either pointer is not justified by a monotonic fact
- the best choice may require remembering many earlier values, which points to hashing or DP
Terminology clues
These prompt words often point toward this concept:
- sorted array
- pair
- palindrome
- from both ends
- in-place
- remove duplicates
- closest sum
- left and right
Problems that use it
- 5. Longest Palindromic Substring
- 11. Container With Most Water
- 15. 3Sum
- 21. Merge Two Sorted Lists
- 25. Reverse Nodes in k-Group
- 28. Find the Index of the First Occurrence in a String
- 42. Trapping Rain Water
- 125. Valid Palindrome
- 160. Intersection of Two Linked Lists
- 167. Two Sum II, Input Array Is Sorted
- 189. Rotate Array
- 443. String Compression
- 647. Palindromic Substrings