Last active
July 10, 2024 07:31
-
-
Save HerringtonDarkholme/3474f0c0a0ae6b78d0767b83ed4f3b83 to your computer and use it in GitHub Desktop.
Cyan's secrete
This file contains 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 findCombinations(numbers, targetSum) { | |
const result = []; | |
function find(currentSum, index, currentCombination) { | |
if (currentSum === targetSum) { | |
result.push([...currentCombination]); | |
return; | |
} | |
if (currentSum > targetSum || index === numbers.length) return; | |
// Include the number at the current index. | |
currentCombination.push(numbers[index]); | |
find(currentSum + numbers[index], index + 1, currentCombination); | |
// Exclude the number at the current index. | |
currentCombination.pop(); | |
find(currentSum, index + 1, currentCombination); | |
} | |
find(0, 0, []); | |
return result; | |
} | |
const numbers = [10, 6, 2, 6, 1, 9, 6, 3, 9, 2, 7, 7, 12, 11]; | |
const combinations = findCombinations(numbers, 63); | |
console.log(combinations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Show the probability of each event.