Source: https://leetcode.com/problems/combination-sum-iv/

Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target. The test cases are generated so that the answer can fit in a 32-bit integer.

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
/*
Recursion Approach
Either we take the current number (and go back to start index) or skip the current number and go to next index
Base Cases
If the current sum is > target or the index is >= nums.length -> return 0
If the current sum === target -> return 1
Recursive Step
takeCombinations = rec(0, currentSum + nums[index])
skipCombinations = rec(index + 1, currentSum)
return takeCombinations + skipCombinations
*/
var combinationSum4 = function(nums, target) {
function rec(currentIndex, currentSum) {
if (currentSum === target) {
return 1;
}
if (currentSum > target || currentIndex >= nums.length) {
return 0;
}
const takeCombinations = rec(0, currentSum + nums[currentIndex]);
const skipCombinations = rec(currentIndex + 1, currentSum);
return takeCombinations + skipCombinations;
}
return rec(0, 0);
};