Skip to content

Instantly share code, notes, and snippets.

@jdgo-mars
Created February 28, 2019 09:16
Show Gist options
  • Save jdgo-mars/36c5fbd9da57ed99f1b308644169fd28 to your computer and use it in GitHub Desktop.
Save jdgo-mars/36c5fbd9da57ed99f1b308644169fd28 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