Skip to content

Instantly share code, notes, and snippets.

@jdgo-mars
Created March 10, 2019 07:08
Show Gist options
  • Save jdgo-mars/ff496b9fd5a70315b2553b8a9f7e1188 to your computer and use it in GitHub Desktop.
Save jdgo-mars/ff496b9fd5a70315b2553b8a9f7e1188 to your computer and use it in GitHub Desktop.
function existsSum(arr, n, lIndex = 0, hIndex = null, iter = 0) {
const low = arr[lIndex];
const high = arr[hIndex || arr.length - 1];
const predicate = low + high;
if(iter === arr.length - 1 ) return false;
if(predicate === n) return true;
if(predicate < n) {
// increment lower index
return existsSum(arr, n, lIndex + 1, hIndex, iter + 1);
} else {
// decrement higher index
return existsSum(arr, n, lIndex, hIndex - 1, iter + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment