LeetCode #167 Medium

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 target and return them as [index1, index2], with index1 < 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.

Constraints
  • 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.
arraytwo-pointersbinary-search
Open on LeetCode ↗
02

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 r left.
  • Sum too small: the left number is too small. Even the largest remaining partner cannot lift it to target. Drop it: move l right.
  • 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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def twoSum(self, numbers, target):
▶3 l, r = 0, len(numbers) - 1
▶4 while l < r:
▶5 s = numbers[l] + numbers[r]
▶6 if s == target:
▶7 return [l + 1, r + 1]
▶8 if s < target:
▶9 l += 1
▶10 else:
▶11 r -= 1
▶12 return []
11L23344557611R1+11=12>9sumtargetpairs left34571113457L on the smallest, R on the largest
L1
R6
target9
Each grid cell is one pair of positions, row for the smaller and column for the larger. The pointers start at the corner cell: the smallest number with the largest.
11L23344557611R1+11=12>9sumtargetpairs left34571113457sum 12 too big → drop 11, move R
L1value 1
R6value 11
sum12
1 + 11 = 12 is above 9. 1 is the smallest partner still in play, so 11 overshoots with every partner. Its whole column of pairs is ruled out.
11L23344557R6111+7=8<9sumtargetpairs left34571113457sum 8 too small → drop 1, move L
L1value 1
R5value 7
sum8
1 + 7 = 8 is below 9. 7 is the largest partner still in play, so no partner can lift 1 to the target. Its whole row of pairs is ruled out.
1123L344557R6113+7=10>9sumtargetpairs left34571113457sum 10 too big → drop 7, move R
L2value 3
R5value 7
sum10
3 + 7 = 10 is above 9. 3 is the smallest partner still in play, so 7 overshoots with every partner. Its whole column of pairs is ruled out.
1123L3445R576113+5=8<9sumtargetpairs left34571113457sum 8 too small → drop 3, move L
L2value 3
R4value 5
sum8
3 + 5 = 8 is below 9. 5 is the largest partner still in play, so no partner can lift 3 to the target. Its whole row of pairs is ruled out.
112334L45R576114+5=9=9sumtargetpairs left34571113457sum 9 hits the target → positions 3 and 4
sum9
answer[3, 4]
4 + 5 = 9, so the pair is found. The array counts from 1, so 0-based indices 2 and 3 become [3, 4]. The highlighted grid cell was never crossed out.
05

Common pitfalls

Returning 0-based positions

✗ Wrong
return [l, r]
✓ Right
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

✗ Wrong
if s < target:
    r -= 1
✓ Right
if s < target:
    l += 1

Moving 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.

06

Edge cases

Two equal values make the pair

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.

Negative numbers and targets

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].

07

Complexity

Time
O(n)
Space
O(1)
Each step moves one pointer by one, so there are at most n - 1 steps. Only the two pointers and one sum are stored.
08

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.