Skip to content

Two Sum II (Sorted Input)

MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a list numbers that is sorted in ascending order and a number target. Find the two different positions whose values add up to target and return their 0-based indices in increasing order.

numbers = [2, 7, 11, 15], target = 9    →  [0, 1]
numbers = [2, 3, 4],      target = 6    →  [0, 2]
numbers = [-1, 0],        target = -1   →  [0, 1]

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • -1000 <= target <= 1000
  • Exactly one valid pair exists.
  • Use only O(1) extra space.