forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination.java
More file actions
66 lines (58 loc) · 2 KB
/
Combination.java
File metadata and controls
66 lines (58 loc) · 2 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.thealgorithms.backtracking;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
/**
* Finds all combinations of a given array using backtracking
*
* @author Alan Piao
@edit Sarthak Shelar
*/
public final class Combination {
private Combination() {
}
/**
* Find all combinations of a given array using backtracking
*
* @param arr the array.
* @param n length of combination
* @param <T> the type of elements in the array.
* @return a list of all combinations of length n. If n == 0, return an empty list.
*/
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
if (n < 0) {
throw new IllegalArgumentException("The combination length cannot be negative.");
}
if (n == 0) {
return Collections.emptyList();
}
T[] array = arr.clone();
Arrays.sort(array); // Sort to maintain consistent order
List<TreeSet<T>> result = new LinkedList<>();
backtrack(array, n, 0, new LinkedList<>(), result);
return result;
}
/**
* Backtrack all possible combinations of a given array
*
* @param arr the array.
* @param n length of the combination
* @param index the starting index.
* @param current current combination under construction
* @param result the list that contains all valid combinations
* @param <T> the type of elements in the array.
*/
private static <T> void backtrack(T[] arr, int n, int index, LinkedList<T> current, List<TreeSet<T>> result) {
if (current.size() == n) {
result.add(new TreeSet<>(current)); // Convert to TreeSet to ensure uniqueness and sorted order
return;
}
for (int i = index; i < arr.length; i++) {
current.add(arr[i]);
backtrack(arr, n, i + 1, current, result);
current.removeLast();
}
}
}