Two Sum II - Input Array Is Sorted
Find the pair that adds up to a target in a sorted array by squeezing in from both ends, with O(1) extra space.
01The problem
numbers is sorted ascending. Find the two numbers that add up to target and return their positions counting from 1, as [index1, index2] with index1 < index2. Exactly one answer exists, and you must use O(1) extra space.
| Input | Output | Why |
|---|---|---|
[2, 7, 11, 15], target 9 | [1, 2] | 2 + 7 |
[2, 3, 4], target 6 | [1, 3] | 2 + 4 |
Constraints: 2 to 30,000 numbers, sorted ascending, exactly one solution.
02How to spot it
- The input is sorted.
- You need a pair with a sum.
- Constant extra space is required.
So: squeeze from both ends.
03Pseudocode
l = 0 ; r = n - 1
while l < r:
sum = a[l] + a[r]
if sum == target: return [l+1, r+1]
if sum < target: l += 1
else: r -= 1- Start with the smallest and the largest.
- Too small? Move the left finger right.
- Too big? Move the right finger left.
04Watch it work
Start with the smallest and the largest. We want a pair that adds up to 10.
Pseudocode
- l = 0 ; r = n - 1
- while l < r:
- sum = a[l] + a[r]
- if sum == target: return [l+1, r+1]
- if sum < target: l += 1
- else: r -= 1
Variables
- l
- 0
- r
- 5
- target
- 10
05Step-by-step dry run
- 11031426384115lr
Start with the smallest and the largest. We want a pair that adds up to 10.
l = 0r = 5target = 10
- 21031426384115lr
1 + 11 = 12 is too big. Only a smaller right number can lower it: move r left.
l = 0r = 5sum = 12
- 31031426384115lr
1 + 8 = 9 is too small. Only a bigger left number can raise it: move l right.
l = 0r = 4sum = 9
- 41031426384115lr
3 + 8 = 11 is too big. Only a smaller right number can lower it: move r left.
l = 1r = 4sum = 11
- 51031426384115lr
3 + 6 = 9 is too small. Only a bigger left number can raise it: move l right.
l = 1r = 3sum = 9
- 61031426384115lr
4 + 6 = 10, the target. Return positions [3, 4] (counting from 1).
l = 2r = 3sum = 10
06Java solution
import java.util.Arrays;
public class TwoSumSorted {
static int[] twoSum(int[] numbers, int target) {
int l = 0, r = numbers.length - 1;
while (l < r) {
int sum = numbers[l] + numbers[r];
if (sum == target) return new int[] {l + 1, r + 1}; // 1-indexed
if (sum < target) l++; // need bigger: move left up
else r--; // need smaller: move right down
}
return new int[] {-1, -1}; // never reached: one answer exists
}
public static void main(String[] args) {
System.out.println(Arrays.toString(twoSum(new int[] {1, 3, 4, 6, 8, 11}, 10)));
System.out.println(Arrays.toString(twoSum(new int[] {2, 7, 11, 15}, 9)));
System.out.println(Arrays.toString(twoSum(new int[] {-1, 0}, -1)));
System.out.println(Arrays.toString(twoSum(new int[] {1, 2, 3, 4, 4, 9}, 8)));
}
}[3, 4] [1, 2] [1, 2] [4, 5]
Tricky lines, decoded
l + 1, r + 1: the problem counts positions from 1. A classic off-by-one trap.sum < target → l++: only increasing the smaller number can raise the sum.
07Why it works
If a[l] + a[r] is too small, a[l] can't be in the answer with any element left of r either, because those are even smaller, so discarding l is safe. The same logic discards r when the sum is too big. Each step removes one impossible candidate, so the answer is never skipped.
Invariant: if an answer exists, both of its indices lie in [l, r].
08Time and space
- Time: O(n). Each step moves one pointer.
- Space: O(1). A hash map also gives O(n) time but uses O(n) memory.
09Edge cases
| Input | Output | Why it matters |
|---|---|---|
[-1, 0], -1 | [1, 2] | negatives |
[2, 3, 4], 6 | [1, 3] | pair at the ends |
[1, 2, 3, 4, 4, 9], 8 | [4, 5] | equal values |
[-3, -1, 0, 2, 5], -4 | [1, 2] | all on the left |
[5, 25, 75], 100 | [2, 3] | pair at the right |
10Common mistakes
- Returning 0-based indices. The judge expects positions from 1.
- Using a HashMap. It works, but ignores the sorted hint and the O(1) space rule.
l <= r. An element can't pair with itself: usel < r.
11Other approaches
| Approach | Time | Space | Use when |
|---|---|---|---|
| All pairs | O(n²) | O(1) | Never |
| Two pointers | O(n) | O(1) | Default for sorted input |
| Binary search per l | O(n log n) | O(1) | Explaining alternatives |
| Hash map | O(n) | O(n) | Unsorted input (LC 1) |
12In the interview
- Count all pairs with sum < target? When
a[l] + a[r] < target, allr − lpairs starting atlcount: add them at once, thenl++. - Three numbers? 3Sum (problem 24): fix one, run this exact loop on the rest.
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. The sum is too small. Which pointer moves?
2. Why l < r and not l <= r?
14Solve it yourself
Try it on LeetCode #167.