Skip to content
BASA DSA

Two Sum II Input Array Is Sorted

MediumTwo Pointers

In a nondecreasing integer array, return the two distinct one-based indices whose values sum to target. Use constant auxiliary space.

Example 1

Input
numbers = [-3,0,4,9]
target = 6
Output
[1,4]

Explanation

-3 + 9 = 6; zero-based positions 0 and 3 become one-based indices 1 and 4.

Example 2

Input
numbers = [2,2]
target = 4
Output
[1,2]

Explanation

The separate 2 occurrences give one-based indices 1 and 2.

Example 3

Input
numbers = [1,4,6,10]
target = 10
Output
[2,3]

Explanation

4 + 6 = 10, selecting one-based indices 2 and 3.

Constraints

  • 2 <= len(numbers) <= 30000
  • Exactly one pair exists

Function contracttwoSumArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…