Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,44 @@
import java.util.List;

/**
* Finds all combinations of 0...n-1 of length k
* This class provides methods to find all combinations of integers from 0 to n-1
* of a specified length k using backtracking.
*/
public final class ArrayCombination {
private ArrayCombination() {
}

/**
* Finds all combinations of length k of 0..n-1 using backtracking.
* Generates all possible combinations of length k from the integers 0 to n-1.
*
* @param n Number of the elements.
* @param k Length of the combination.
* @return A list of all combinations of length k.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
* @return A list containing all combinations of length k.
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
*/
public static List<List<Integer>> combination(int n, int k) {
if (n < 0 || k < 0 || k > n) {
throw new IllegalArgumentException("Wrong input.");
throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n.");
}

List<List<Integer>> combinations = new ArrayList<>();
combine(combinations, new ArrayList<>(), 0, n, k);
return combinations;
}

/**
* A helper method that uses backtracking to find combinations.
*
* @param combinations The list to store all valid combinations found.
* @param current The current combination being built.
* @param start The starting index for the current recursion.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
*/
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
if (current.size() == k) { // Base case: combination found
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
// Base case: combination found
if (current.size() == k) {
combinations.add(new ArrayList<>(current));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ void testCombinationThrows(int n, int k) {

private static Stream<Arguments> regularInputs() {
return Stream.of(Arguments.of(0, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 1, List.of(List.of(0))), Arguments.of(3, 0, List.of(new ArrayList<Integer>())), Arguments.of(3, 1, List.of(List.of(0), List.of(1), List.of(2))),
Arguments.of(4, 2, List.of(List.of(0, 1), List.of(0, 2), List.of(0, 3), List.of(1, 2), List.of(1, 3), List.of(2, 3))));
Arguments.of(4, 2, List.of(List.of(0, 1), List.of(0, 2), List.of(0, 3), List.of(1, 2), List.of(1, 3), List.of(2, 3))),
Arguments.of(5, 3, List.of(List.of(0, 1, 2), List.of(0, 1, 3), List.of(0, 1, 4), List.of(0, 2, 3), List.of(0, 2, 4), List.of(0, 3, 4), List.of(1, 2, 3), List.of(1, 2, 4), List.of(1, 3, 4), List.of(2, 3, 4))),
Arguments.of(6, 4,
List.of(List.of(0, 1, 2, 3), List.of(0, 1, 2, 4), List.of(0, 1, 2, 5), List.of(0, 1, 3, 4), List.of(0, 1, 3, 5), List.of(0, 1, 4, 5), List.of(0, 2, 3, 4), List.of(0, 2, 3, 5), List.of(0, 2, 4, 5), List.of(0, 3, 4, 5), List.of(1, 2, 3, 4), List.of(1, 2, 3, 5), List.of(1, 2, 4, 5),
List.of(1, 3, 4, 5), List.of(2, 3, 4, 5))));
}

private static Stream<Arguments> wrongInputs() {
return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100));
return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100), Arguments.of(3, 4));
}
}