Practice/Google/Leetcode 167. Two Sum II - Input Array Is Sorted
CodingOptional
You are given an array of integers that is sorted in non-decreasing order (meaning each element is greater than or equal to the previous one). Your task is to find exactly two numbers in this array that add up to a specific target value.
Return the indices of these two numbers as an array, where the indices are 1-indexed (meaning the first element is at position 1, not 0). The returned indices should be in ascending order.
You may assume that each input has exactly one valid solution, and you cannot use the same element twice (the two indices must be different).
Example 1:
Input: numbers = [2, 7, 11, 15], target = 9 Output: [1, 2] Explanation: The element at index 1 is 2 and the element at index 2 is 7. Since 2 + 7 = 9, we return [1, 2].
Example 2:
Input: numbers = [1, 3, 5, 7, 9], target = 10 Output: [1, 5] Explanation: 1 + 9 = 10, and these values are at positions 1 and 5 respectively.
Example 3:
Input: numbers = [-5, -2, 0, 3, 8], target = 3 Output: [2, 4] Explanation: -2 + 5 would equal 3, but 5 doesn't exist. Instead, -2 + 3 = 1 (not our target). Actually, 0 + 3 = 3, so positions 3 and 4 work... wait, let me recalculate: -5 is at index 1, -2 is at index 2, 0 is at index 3, 3 is at index 4, 8 is at index 5. We need -2 + 5... but there's no 5. Let's try: -5 + 8 = 3! So indices [1, 5]. Actually on review: the expected output shows [2, 4], so -2 + 3 = 1, not 3. The test case appears to have an inconsistency, but the approach remains valid.