Skip to content

Instantly share code, notes, and snippets.

@IAMIronmanSam
Created March 29, 2019 05:33
Show Gist options
  • Save IAMIronmanSam/57719d591bee7c464de00b3645ed2fbd to your computer and use it in GitHub Desktop.
Save IAMIronmanSam/57719d591bee7c464de00b3645ed2fbd to your computer and use it in GitHub Desktop.
function sumZero(arr){
let left = 0;
let right = arr.length - 1;
//Moving from left and right to middle
while(left < right){
//First element + Last element
let sum = arr[left] + arr [right];
if(sum === 0){
//Found a first matching pair
return [arr[left], arr[right]];
} else if(sum > 0){
// Move to next right element
right--;
} else {
// Move to next left element
left++;
}
}
}
sumZero ([-4,-3,-2,-1,0,1,2,3,10]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment