forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
41 lines (31 loc) · 930 Bytes
/
Combinations.java
File metadata and controls
41 lines (31 loc) · 930 Bytes
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
package Backtracking;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 26/10/18
* Time - 12:29 PM
*/
public class Combinations {
public void addCombinations(ArrayList<ArrayList<Integer>> r, int pos, int N, int K, ArrayList<Integer> temp){
if(temp.size() == K){
r.add(new ArrayList<>(temp));
}
else if(temp.size() > K){
return;
}
for(int i=pos;i<=N;i++){
temp.add(i);
addCombinations(r,i+1,N,K,temp);
temp.remove(temp.size()-1);
}
}
public ArrayList<ArrayList<Integer>> combine(int A, int B) {
ArrayList<ArrayList<Integer>> r = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
addCombinations(r,1,A,B,temp);
return r;
}
public static void main(String[] args) {
System.out.println(new Combinations().combine(2,1));
}
}