algorythms
Two Pointers
LC #167Medium

Two Sum II - Input Array Is Sorted

Two Pointers
AmazonGoogleApple

Problem

Find two numbers in a sorted array that add up to a target. Return their 1-indexed positions.

arraytwo-pointersbinary-search

Constraints

  • 2 ≤ n ≤ 3 × 10⁴
  • -1000 ≤ numbers[i] ≤ 1000
  • Sorted in non-decreasing order
  • Exactly one solution guaranteed

Example

Inputnumbers = [2, 7, 11, 15], target = 9
Output[1, 2]
Why

numbers[1] + numbers[2] = 2 + 7 = 9 (1-indexed)

Hints — reveal one at a time