v9.1.1
Menu

Two Sum II - Sorted Input

mediumFREEArrays & SortingAmazon DSA

Given a 1-indexed sorted array numbers and a target, find two numbers that add up to target. Return their 1-indexed positions as space-separated integers.

There is exactly one solution. You may not use the same element twice.

Example

Input
2,7,11,15
9
Output
1 2
Reasoning
  • The input array is 1-indexed and sorted: [2,7,11,15][2, 7, 11, 15].
  • We need to find two numbers that add up to the target 99, so we look for a pair of numbers in the array that satisfy this condition: 2+7=92 + 7 = 9.
  • The positions of these numbers in the array are 11 and 22, respectively, since the array is 1-indexed.
  • The final output is the space-separated positions: 121 2.

Constraints

  • 2 <= len(numbers) <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order

Learn this in

More from Amazon DSA

solution.py

Test results

Sign in to run your code against the test cases.