Find two numbers in a sorted array whose sum equals a target, and return their 1-based positions.
Two Sum II
gfgYou are given a 1-indexed sorted integer array numbers in non-decreasing order and an integer target.
Find two distinct elements in numbers such that their sum is exactly target.
Return the indices of the two numbers as a pair [index1, index2], where index1 < index2.
It is guaranteed that exactly one valid pair exists.
numbers: a sorted integer array in non-decreasing ordertarget: an integerThe function should return two indices i and j such that numbers[i] + numbers[j] = target and i < j (using 1-based indexing).
Return an array of two integers: [index1, index2].
2 <= numbers.lengthnumbers is sorted in non-decreasing order1 <= index1 < index2 <= numbers.lengthExample 1
Input
numbers = [2,7,11,15], target = 9
Output
[1,2]
Explanation
The first two numbers add up to 9, so the 1-based indices are returned.
Example 2
Input
numbers = [2,3,4], target = 6
Output
[1,3]
Explanation
2 + 4 = 6, so the correct answer is the indices of those values.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.