Problem
Given an array sorted in non-decreasing order and a target number, find the indices of the two values from the array that sum up to the given target number.
Example
{
"numbers": [1, 2, 3, 5, 10],
"target": 7
}
Output:
[1, 3]
Solution
function pair_sum_sorted_array(numbers, target) {
let [start, end] = [0, numbers.length-1];
let result = [];
while(start < end){
const sum = numbers[start] + numbers[end];
if(sum < target){
start++;
}else if(sum > target){
end--;
}else{
result = [start++, end--];
break;
}
}
return result.length ? result : [-1,-1];
}