Created
March 29, 2019 05:33
-
-
Save IAMIronmanSam/57719d591bee7c464de00b3645ed2fbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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