Loading...
You are given an integer array numbers that is already sorted in non-decreasing order, and an integer target.
Find the two positions whose values add up to target. Indexes are 0-based, so the first value of numbers sits at index 0. Return the two indexes as [i, j] with i < j, that is, in increasing order.
The two positions must be different, so a single element can never be paired with itself; two equal values are fine as long as they sit at different positions. Exactly one pair of positions adds up to target, so the answer is unique.
Input: numbers = [2,7,11,15], target = 9
Output: [0,1]
Explanation: numbers[0] + numbers[1] = 2 + 7 = 9, so the answer is [0, 1].
Input: numbers = [2,3,4], target = 6
Output: [0,2]
Explanation: numbers[0] + numbers[2] = 2 + 4 = 6. The middle value takes no part in the answer.
Input: numbers = [-1,0], target = -1
Output: [0,1]
Explanation: numbers[0] + numbers[1] = -1 + 0 = -1. With only two values there is a single candidate pair, and its 0-based indexes are 0 and 1.
numbers.length ≤3⋅104numbers[i] ≤1000target ≤1000numbers is sorted in non-decreasing ordertargetClick "Run" to test with sample cases or "Submit" to run all tests.