# LeetCode – Combination Sum (Java)

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times.

Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, ... , ak) must be in non-descending order. (ie, a1 <= a2 <= ... <= ak). The solution set must not contain duplicate combinations. For example, given candidate set 2,3,6,7 and target 7, A solution set is:

```[7]
[2, 2, 3]
```

Thoughts

The first impression of this problem should be depth-first search(DFS). To solve DFS problem, recursion is a normal implementation.

Note that the candidates array is not sorted, we need to sort it first.

Java Solution

```public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();   if(candidates == null || candidates.length == 0) return result;   ArrayList<Integer> current = new ArrayList<Integer>(); Arrays.sort(candidates);   combinationSum(candidates, target, 0, current, result);   return result; }   public void combinationSum(int[] candidates, int target, int j, ArrayList<Integer> curr, ArrayList<ArrayList<Integer>> result){ if(target == 0){ ArrayList<Integer> temp = new ArrayList<Integer>(curr); result.add(temp); return; }   for(int i=j; i<candidates.length; i++){ if(target < candidates[i]) return;   curr.add(candidates[i]); combinationSum(candidates, target - candidates[i], i, curr, result); curr.remove(curr.size()-1); } }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• pjpal

What would be the running time of this algorithm?

• Александр Ефремов

Because this is the main idea of backtracking – try option (curr.add(candidate)), then backtrack – curr.remove(cur.size()-1)

• Didier

This solution is incorrect. If candidates are [2, 3, 3, 6, 7]. The returned lists would be:
[2, 2, 3]
[2, 2, 3]
[7]

May need to add some condition to exclude the repeated entry in candidates.

• gowtham Bk

Why can’t we just add curr to result ? Why do we use temp ?

• ryanlr

I don’t think so, because duplicates are allowed in a combination set, i. e., the same number csn be used twice.

• divya

Shouldn’t it be i+1, 3rd parameter here->combinationSum(candidates, target – candidates[i], i, curr, result);

• Surbhi Motghare

Great solution. why are we removing the last element from curr. (last line where curr.remove(curr.size()-1) ? )

• Dexter

public static void sum(int[] set,int s){

int [][]dp = new int[s+1][set.length];

for(int i=0;i < set.length; i++){

for(int j = 0; j j){

dp[j][i] = 0;

continue;

}

if(set[i] == j){

dp[j][i] = i+1;

continue;

}

int remaining = j-set[i];

int k = i;

while(k >= 0){

if(dp[remaining][k] > 0){

dp[j][i] = k+1;

break;

}

k–;

}

}

}

int l=0;

while(l 0){

int sum = s;

int col = l;

System.out.print(“[“);

while(sum > 0){

System.out.print(set[col]+”,”);

int temp = col;

col = dp[sum][col]-1;

sum -= set[temp];

}

System.out.print(“]”);

}

l++;

System.out.println(“”);

}

}

• Jeffrey Ni

Java always pass parameters by value. Here curr is a refferrece to the array object, if you don’t copy it but add it to the result directly, later when you modify curr, it will change the list inside result. You may end up with all same lists in result. You can self-test it.

• Lean Lee

I wonder why we need make a temp ArrayList and then copy the curr Arraylist and then add temp into result, why just add curr into the result directly?