-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFindAllDistinctCombinationsOfAGivenLength.java
49 lines (43 loc) · 1.36 KB
/
FindAllDistinctCombinationsOfAGivenLength.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Main
{
public static void findCombinations(int[] A, int n, int k,
Set<List<Integer>> subarrays,
List<Integer> out)
{
// invalid input
if (A.length == 0 || k > n) {
return;
}
// base case: combination size is `k`
if (k == 0) {
subarrays.add(new ArrayList<>(out));
return;
}
// start from the next index till the first index
for (int i = n - 1; i >= 0; i--)
{
// add current element `A[i]` to the output and recur for next index
// `i-1` with one less element `k-1`
out.add(0, A[i]);
findCombinations(A, i, k - 1, subarrays, out);
out.remove(0); // backtrack
}
}
public static Set<List<Integer>> findCombinations(int[] A, int k)
{
Set<List<Integer>> subarrays = new HashSet<>();
findCombinations(A, A.length, k, subarrays, new ArrayList<>());
return subarrays;
}
public static void main(String[] args)
{
int[] A = { 1, 2, 3 };
int k = 2;
// process elements from right to left
System.out.println(findCombinations(A, k));
}
}