Algorithm
Problem Name: 167. Two Sum II - Input Array Is Sorted
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:
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:
Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
Constraints:
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers
is sorted in non-decreasing order.-1000 <= target <= 1000
- The tests are generated such that there is exactly one solution.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
int *result;
int i, j, total;
result = malloc(2 * sizeof(int));
//assert(result);
*returnSize = 0;
i = 0;
j = numbersSize - 1;
while (i < j) {
total = numbers[i] + numbers[j];
if (total > target) {
j --;
} else if (total < target) {
i ++;
} else {
result[0] = i + 1;
result[1] = j + 1;
*returnSize = 2;
break;
}
}
return result;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int[] twoSum(int[] numbers, int target) {
int leftIdx = 0;
int rightIdx = numbers.length - 1;
while (leftIdx < rightIdx) {
int currSum = numbers[leftIdx] + numbers[rightIdx];
if (currSum == target) {
return new int[]{leftIdx + 1, rightIdx + 1};
} else if (currSum > target) {
rightIdx--;
} else {
leftIdx++;
}
}
return new int[]{0};
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const twoSum = function(numbers, target) {
const res = [];
let remaining;
let next = 0;
for (let i = 0; i < numbers.length; i++) {
remaining = target - numbers[i];
next = i + 1;
while (next < numbers.length && numbers[next] <= remaining) {
if (numbers[next] === remaining) {
res.push(i + 1, next + 1);
break;
}
next += 1;
}
}
return res;
};
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left=numbers[0]
right=numbers[-1]
i,j=0,0
while True:
sum=left+right
if sum>target:
j+=1
right=numbers[-1-j]
if sum
Copy The Code &
Try With Live Editor
Input
Output