Description
Given a 1-indexed array of integers 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.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Example 1:
1Input: numbers = [2,7,11,15], target = 9
2Output: [1,2]
3Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
1Input: numbers = [2,3,4], target = 6
2Output: [1,3]
3Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:
1Input: numbers = [-1,0], target = -1
2Output: [1,2]
3Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
Constraints:
2 <= numbers.length <= 3 * 10^4-1000 <= numbers[i] <= 1000numbersis sorted in non-decreasing order.-1000 <= target <= 1000- The tests are generated such that there is exactly one solution.
Solution
In this case, it’s mentioned that the input array is sorted, so we know that values increase from left to right. The better idea would be to use two pointers and use them to navigate closer to the result. In this case, we have sorted array as input and this allows us to use two pointers, because we know in which direction the numbers will be larger. The basic algorithm for this looks like below. The important point to remember is that this array is 1-indexed. So, we initialize left and right offset by 1.
1Initialize one pointer left = 1
2Initialize another pointer right = nums.length
3while left less than right
4 Check if nums[left-1] + nums[right-1] greater than target
5 right = right + 1
6 Else if nums[left-1] + nums[right-1] less than target
7 left = left + 1
8 Else
9 return [left, right]
10If we didn't find a pair, return [-1, -1]
Let’s convert this pseudo code into actual Java program.
1class Solution {
2 public int[] twoSum(int[] numbers, int target) {
3 int left = 1, right = numbers.length;
4 while (left < right) {
5 int sum = numbers[left - 1] + numbers[right - 1];
6 if (sum > target) {
7 right--;
8 } else if (sum < target) {
9 left++;
10 } else {
11 return new int[]{left, right};
12 }
13 }
14 return new int[]{-1, -1};
15 }
16}


Comments