Skip to content

Instantly share code, notes, and snippets.

@juanigallo
Created September 8, 2017 18:08
Show Gist options
  • Save juanigallo/9740c8ca5ef97b611325796c15b68e83 to your computer and use it in GitHub Desktop.
Save juanigallo/9740c8ca5ef97b611325796c15b68e83 to your computer and use it in GitHub Desktop.
Check sum of ordered array with JavaScript
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