Created
January 14, 2017 18:13
-
-
Save kutyel/eba5fe9b334586889bc9397365d39234 to your computer and use it in GitHub Desktop.
Sum of 'Z' numbers in an array
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
/* | |
* Complete the function below. | |
*/ | |
function isSumPossibleZ(numbers, expectedSum, numbersToSum) { | |
function f(index, expectedSum, numbersToSum) { | |
if (numbersToSum === 0) return expectedSum === 0; | |
for (var length = numbers.length; index < length; index++) { | |
if (f(index + 1, expectedSum - numbers[index], numbersToSum - 1)) { | |
return 1; | |
} | |
} | |
return 0; | |
} | |
numbers.sort(); | |
return f(0, expectedSum, numbersToSum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment