Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

&nbsp;
Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.


Example 2:

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]


Example 3:

Input: candidates = [2], target = 1
Output: []


&nbsp;
Constraints:


	1 &lt;= candidates.length &lt;= 30
	1 &lt;= candidates[i] &lt;= 200
	All elements of candidates are distinct.
	1 &lt;= target &lt;= 500


class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:

        path = []
        answer = []
        def dp(idx, total):
            if total == target:
                answer.append(path[:])
                return
            if total > target:
                return

            for i in range(idx, len(candidates)):
                path.append(candidates[i])
                dp(i, total + candidates[i])
                path.pop()

        dp(0, 0)
        return answer
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> cur = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(candidates);
        dfs(0, candidates, target, 0, cur, result);
        return result;
    }
    public void dfs(int start, int[] candidates, int target, int sum, List<Integer> cur, List<List<Integer>> result){
        if(sum == target){
            result.add(new ArrayList<>(cur));
            return;
        }
        for(int i = start; i < candidates.length; i++) {
            if(sum + candidates[i] <= target) {
              cur.add(candidates[i]);
              dfs(i, candidates, target, sum + candidates[i], cur, result);
              cur.remove((cur.size()- 1));
            }
        }
        return;
    }
}
class Solution {
    void combination(vector<int>& candidates, int target, vector<int> currComb, int currSum, int currIndex, vector<vector<int>>& ans){
        if(currSum>target) return; //backtrack
        if(currSum==target){
            ans.push_back(currComb); //store the solution and backtrack
            return;
        }

        for(int i=currIndex; i<candidates.size(); i++){ //try all possible options for the next level
            currComb.push_back(candidates[i]); //put 1 option into the combination
            currSum+=candidates[i];
            combination(candidates, target, currComb, currSum, i, ans); //try with this combination, whether it gives a solution or not.
            currComb.pop_back(); //when this option backtrack to here, remove this and go on to the next option.
            currSum-=candidates[i];
        }

    }
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        vector<int> currComb;
        combination(candidates, target, currComb, 0, 0, ans);
        return ans;
    }
};
function recursion(index, list, target, res, arr){
    if(index == arr.length){
        if(target == 0){
            res.push([...list]);
        }
        return;
    }

    if(arr[index] <= target){

         list.push(arr[index]);

         recursion(index, list, target - arr[index], res, arr);

         list.pop();

    }

    recursion(index + 1, list, target, res, arr);

}
var combinationSum = function(candidates, target) {
    let res = [];
    recursion(0, [], target, res, candidates);
    return res;
};
Combination Sum