You're given a sorted array of integers and need to find two numbers that add up to a specific target. This is a classic problem that tests your ability to leverage the sorted property of the input.
Given a 1-indexed array numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 โค index1 < index2 โค numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
Key constraints:
- The tests guarantee that there is exactly one solution
- You may not use the same element twice
- Your solution must use only constant extra space
Example: If numbers = [2,7,11,15] and target = 9, return [1,2] because numbers[1] + numbers[2] = 2 + 7 = 9.
Input & Output
Visualization
Time & Space Complexity
We potentially check every pair of elements, leading to nร(n-1)/2 comparisons
Only using a few variables, no additional data structures
Constraints
- 2 โค numbers.length โค 3 * 104
- -1000 โค numbers[i] โค 1000
- -1000 โค target โค 1000
- The array is sorted in non-decreasing order
- Exactly one solution exists
- You cannot use the same element twice