Created
February 28, 2019 09:16
-
-
Save jdgo-mars/36c5fbd9da57ed99f1b308644169fd28 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 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