Write a function called sumZero which accepts a sorted array of integers. The function should find the first pair where the sum is 0. Return an array that includes both values that sum to zero or undefined if a pair doesn’t exist.
multi pointer pattern
Following is sample javascript code.
function sumZero(arr) {
let left = 0;
let right = arr.length - 1;
while(left < right){
let sum = arr[left] + arr[right];
if(sum === 0) {
return [arr[left], arr[right]];
} else if(sum>0) {
right --;
} else if(sum === 0){
console.log("hey this is false positive because 0 - 0 = 0");
return "+/-0"
}else {
left ++;
}
}
};
input:
console.log(sumZero([-4,-3,-2,-1,0,1,2,3,10]));
Output:
[ -3, 3 ]