-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination_Sum.java
35 lines (30 loc) · 1013 Bytes
/
Combination_Sum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package AllCodes;
import java.util.*;
public class Combination_Sum {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>>ans=new LinkedList<>();
List<Integer>container=new LinkedList<>();
helper(candidates,0,target,ans,container);
return ans;
}
public void helper(int[] arr, int ind, int t, List<List<Integer>>ans, List<Integer>container){
if(ind==arr.length){
if(t==0){
ans.add(new LinkedList<>(container));
}
return;
}
boolean flag=false;
//Condition to pick current element
if(arr[ind]<=t){
container.add(arr[ind]);
flag=true;
helper(arr,ind,t-arr[ind],ans,container);
}
if(flag){
container.remove(container.size()-1);
}
//Not pick the current element
helper(arr,ind+1,t,ans,container);
}
}