Find two numbers in a sorted array whose sum equals a target, and return their 1-based positions.
Two Sum II
gfgProblem
You 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.
Notes
- Use the array as provided; do not sort it again.
- The answer must use 1-based indexing.
- Because the array is sorted, there is a more efficient approach than checking every pair.
Input Format
numbers: a sorted integer array in non-decreasing ordertarget: an integer
The function should return two indices i and j such that numbers[i] + numbers[j] = target and i < j (using 1-based indexing).
Output Format
Return an array of two integers: [index1, index2].
Constraints
2 <= numbers.lengthnumbersis sorted in non-decreasing order- There is exactly one solution
1 <= index1 < index2 <= numbers.length
Example 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
Unlock deeper context for this problem
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.