Created
March 10, 2019 07:08
-
-
Save jdgo-mars/ff496b9fd5a70315b2553b8a9f7e1188 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