Intuition
You have a sorted list of prices and a gift card for exactly 34. Which two items use it up?
The obvious way checks every pair: n(n − 1)/2 sums. But the list is sorted, and that tells you which way to move. Start with the cheapest and the most expensive item. If together they cost too little, the cheapest item can’t work with anything, because the most expensive is already the best partner it has. Drop it and move up. If they cost too much, the most expensive item can’t work with anything either, so drop it and move down.
How it works
- Put
leftat the first index andrightat the last. - Add the two values.
- If the sum equals the target, you’re done.
- If it’s too small, move
leftone step right, to a bigger value. - If it’s too big, move
rightone step left, to a smaller value. - Stop when the pointers meet: there’s no pair.
Each step moves one pointer one place inward, so the loop runs at most n − 1 times.
Why it’s correct
When the sum is too small, every pair using a[left] is too small too, since a[right] is the largest value still in play. So a[left] can be crossed out for good, and the figure strikes it through. The same reasoning crosses out a[right] when the sum is too big. Each step rules out a value without ever skipping the answer.
That’s the same trick as binary search: use the sorted order to throw away part of the input after one comparison.
Complexity
| Approach | Sums checked | Extra space |
|---|---|---|
| Nested loops, every pair | n(n − 1)/2 | O(1) |
| Hash set of values seen | n | O(n) |
| Two pointers on sorted input | at most n − 1 | O(1) |
If the input isn’t sorted yet, sorting costs O(n log n), and a hash map may be the better tool.
Where it shows up
- Pair or triple sums: two sum on sorted input, three sum (fix one value, run two pointers on the rest).
- Reversing in place: swap
a[left]anda[right], move both inward. Also how you check for a palindrome. - Merging two sorted arrays: one pointer per array, as in merge sort.
- Removing duplicates in place: a slow pointer writes, a fast pointer reads.
- Container with most water: move whichever side is shorter.
Common mistakes
- Using it on unsorted input. The “too small, move left” reasoning only holds when the array is sorted.
left <= rightinstead ofleft < right. With<=, the loop can pair a value with itself.- Moving both pointers at once. Only the pointer whose value was ruled out should move.
Check yourself
Check yourself
In [1, 4, 6, 9, 12], looking for a sum of 13, the first sum is 1 + 12 = 13. What happens?
The very first sum matches, so the pair is at indexes 0 and 4. One check.
Check yourself
In [1, 4, 6, 9, 12], looking for 15, the first sum is 1 + 12 = 13. Which pointer moves?
13 is too small. 1 paired with the biggest value is still short, so 1 can’t be in any answer. Move left to 4, and 4 + 12 = 16 is next.
Sources
- Steven Halim and Felix Halim, Competitive Programming 3 (2013).
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, exercise 2.3-7 (3rd edition).
Finished this page?
Your list