-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCombinationsSum.java
More file actions
45 lines (35 loc) · 1.24 KB
/
CombinationsSum.java
File metadata and controls
45 lines (35 loc) · 1.24 KB
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
36
37
38
39
40
41
42
43
44
45
package Backtracking;
import java.util.*;
/**
* Author - archit.s
* Date - 27/10/18
* Time - 11:51 AM
*/
public class CombinationsSum {
public void addMatchComb(ArrayList<ArrayList<Integer>> r, ArrayList<Integer> input, int pos ,
int currentSum, int reqSum, ArrayList<Integer> temp, Map<ArrayList<Integer>, Boolean> map){
if(currentSum == reqSum && !map.containsKey(temp)){
ArrayList<Integer> t = new ArrayList<>(temp);
r.add(t);
map.put(t, true);
return;
}else if(currentSum > reqSum){
return;
}
for(int i=pos;i<input.size();i++){
currentSum += input.get(i);
temp.add(input.get(i));
addMatchComb(r,input,i,currentSum,reqSum,temp,map);
currentSum-=input.get(i);
temp.remove(temp.size()-1);
}
}
public ArrayList<ArrayList<Integer>> combinationSum(ArrayList<Integer> A, int B) {
ArrayList<ArrayList<Integer>> r = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
Map<ArrayList<Integer>, Boolean> map = new HashMap<>();
Collections.sort(A);
addMatchComb(r,A,0,0,B,temp,map);
return r;
}
}