Two Sum II - Input Array Is Sorted
Two Sum II - Input Array Is Sorted is LeetCode 167 (Medium). numbers is sorted in non-decreasing order and is 1-indexed.
- Find the two different positions whose values add up to
targetand return them as[index1, index2], withindex1 < index2. - Exactly one such pair exists, and the same element cannot be used twice.
- The solution may use only constant extra space.
The sorted order is the whole point: it lets you discard numbers without ever storing them.
- 2 <= numbers.length <= 3 * 10⁴
- -1000 <= numbers[i] <= 1000
- numbers is sorted in non-decreasing order.
- -1000 <= target <= 1000
- The tests are generated such that there is exactly one solution.
Intuition
Put one pointer on the smallest number and one on the largest, and look at their sum.
- Sum too big: the right number is too large. Paired with the smallest number still in play it already overshoots, so paired with anything else it overshoots too. Drop it: move
rleft. - Sum too small: the left number is too small. Even the largest remaining partner cannot lift it to
target. Drop it: movelright. - Sum equal: that is the pair.
Every comparison rules out one whole number, so the pointers meet the answer in at most n - 1 steps.
A sorted array plus "find two values with a given sum" means two pointers from the ends. Sorting is what makes "too big" and "too small" tell you which end to move.
Approach
Before reading on, put pointers at both ends and decide which one must move when the sum is too large, and why the number it leaves can never be part of the answer. Aim for O(n) time and O(1) space.
Start at both ends
Set l = 0 and r = n - 1. These are the smallest and largest numbers, so their sum is the widest starting guess from which you can only move inward.
Compare the sum with the target
Compute s = numbers[l] + numbers[r]. If s == target, return [l + 1, r + 1]; the problem counts positions from 1, so each 0-based index is shifted by one.
Drop one number and repeat
If s < target, move l right; if s > target, move r left. Each move takes exactly one number out of play, and the loop runs while l < r, so one position is never paired with itself.
Why the pair is never skipped
Picture every pair (i, j) with i < j as a cell in a grid. Moving l crosses out row l, and moving r crosses out column r, and each crossed-out line held no valid pair. The answer cell is never crossed out, so the pointers must land on it.
Code in Python, C++ and Java
Common pitfalls
Returning 0-based positions
return [l, r]
return [l + 1, r + 1]
The array is 1-indexed in this problem. For numbers = [2, 7, 11, 15] and target = 9 the expected answer is [1, 2], and [0, 1] is marked wrong.
Moving the wrong pointer when the sum is too small
if s < target:
r -= 1if s < target:
l += 1Moving r left makes the sum smaller still. For [2, 7, 11, 15] and target = 18, the start sum 17 is too small; moving r drops 15 and the scan never reaches 7 + 11, so it returns nothing.
Edge cases
For [3, 3] and target = 6, the first sum already matches and the answer is [1, 2]: two different positions, even with the same value.
Nothing changes, because only the sorted order is used. For [-3, -1, 0, 2, 6] and target = -4, r walks left three times and the answer is [1, 2].
Complexity
Two Sum II - Input Array Is Sorted FAQ
Why not use the hash map from the unsorted version?
It still works in O(n) time, but it stores up to n numbers, which breaks the constant-space rule. It also ignores the sorted order, which is the information this problem hands you for free.
Would binary search for each partner be faster?
No. For each numbers[i] you can binary search target - numbers[i] in the part to its right, which is O(n log n) time and O(1) space. Two pointers reaches O(n) with the same space, so binary search is only worth it when you must answer many different targets against one array.