Walk in from both ends of a sorted array — the sum tells you exactly which side is hopeless, and only one direction can ever help.
Given a sorted array and a target, find the pair of values that adds up to it, and return their positions. A brute-force scan checks every pair, which costs and never uses the fact that the array is sorted.
Two pointers gets it down to a single pass by starting at the two ends and proving, at every step, that one of them can be discarded for good.
Why the sum tells you which pointer to move
Discard the extreme pointer
- If the sum is too big, the right value is too big to pair with
left— and sinceleftis already the smallest value left in play, that value is too big for every remaining candidate. - If the sum is too small, the same argument runs in reverse: the
left value is too small to pair with anything left of
right. - Each comparison rules out an entire diagonal of pairs, not just the one just tested.
What it does not prove
- It doesn't say the pointer's next position is the answer — only that keeping the old one is hopeless.
- It needs a single, fixed target and a total order (
>/<) to compare against — it doesn't extend to "closest sum" without tracking a running best instead of stopping early.
- "sorted array" or "input array is sorted"
- a single target sum, with exactly one valid pair guaranteed
- asked to return positions/indices, not just whether a pair exists
- "constant extra space" ruling out a hash map
The pattern
- 1Place pointers at index
0andn - 1. - 2Compare the sum at the current pair against the target.
- 3
Sum too big → move
rightleft. Sum too small → moveleftright. - 4Stop when the pointers meet, or the sum matches.
Complexity
Each element is visited at most once as the pointers close in.
Just the two indices — no hash map, no auxiliary array.
The unsorted version of this problem needs a hash map — time but also space. Sorting first (if it isn't already) trades that space back for up front.
Common pitfalls
Off-by-one on the return format
The classic sorted Two Sum asks for 1-indexed positions, not array indices — a correct pointer walk with the wrong offset still fails every test.
Moving the wrong pointer
Moving left when the sum is too big (or right when it's too
small) inverts the invariant and can walk straight past the answer
without ever detecting it.
Using `left <= right` as the loop guard
A pair needs two distinct indices — letting the pointers land on the same slot means checking a value against itself.
Assuming order that isn't there
If the input isn't actually sorted by value (or is sorted by something else, like original index), the pointers silently give the wrong answer instead of an error.
Variants worth knowing
- 1
Unsorted Two Sum — no order to exploit, so trade the space for a hash map instead.
- 2
3Sum — fix one value, then run this exact pattern on the rest with a moving target.
- 3
Closest to target — track the best difference seen so far instead of stopping at an exact match.

