Created
September 8, 2017 18:08
-
-
Save juanigallo/9740c8ca5ef97b611325796c15b68e83 to your computer and use it in GitHub Desktop.
Check sum of ordered array with JavaScript
This file contains 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 checkSum(arr, sum) { | |
let low = 0, | |
high = arr.length - 1; | |
while(low < high) { | |
let s = arr[low] + arr[high]; | |
if (s === sum) return true | |
if (s < sum) { | |
low = low + 1 | |
} | |
if (s > sum) { | |
high = high - 1 | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment