Skip to content

Update Combination.java #6320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/main/java/com/thealgorithms/backtracking/Combination.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
import java.util.TreeSet;

/**
* Finds all permutations of given array
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
* 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 given array using backtracking
* Find all combinations of a given array using backtracking
*
* @param arr the array.
* @param n length of combination
* @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 null.
* @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) {
Expand All @@ -29,39 +32,35 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
if (n == 0) {
return Collections.emptyList();
}

T[] array = arr.clone();
Arrays.sort(array);
Arrays.sort(array); // Sort to maintain consistent order

List<TreeSet<T>> result = new LinkedList<>();
backtracking(array, n, 0, new TreeSet<T>(), result);
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 currSet set that tracks current combination
* @param result the list contains all combination.
* @param <T> the type of elements in the 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 backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + n - currSet.size() > arr.length) {
return;
}
if (currSet.size() == n - 1) {
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
result.add(new TreeSet<>(currSet));
currSet.remove(arr[i]);
}
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++) {
currSet.add(arr[i]);
backtracking(arr, n, i + 1, currSet, result);
currSet.remove(arr[i]);
current.add(arr[i]);
backtrack(arr, n, i + 1, current, result);
current.removeLast();
}
}
}
Loading