Skip to content

Commit 32bf532

Browse files
authored
refactor: Enhance docs, add more tests in ArrayCombination (#5841)
1 parent adf21ab commit 32bf532

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

+20-8
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
import java.util.List;
55

66
/**
7-
* Finds all combinations of 0...n-1 of length k
7+
* This class provides methods to find all combinations of integers from 0 to n-1
8+
* of a specified length k using backtracking.
89
*/
910
public final class ArrayCombination {
1011
private ArrayCombination() {
1112
}
1213

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

2527
List<List<Integer>> combinations = new ArrayList<>();
2628
combine(combinations, new ArrayList<>(), 0, n, k);
2729
return combinations;
2830
}
2931

32+
/**
33+
* A helper method that uses backtracking to find combinations.
34+
*
35+
* @param combinations The list to store all valid combinations found.
36+
* @param current The current combination being built.
37+
* @param start The starting index for the current recursion.
38+
* @param n The total number of elements (0 to n-1).
39+
* @param k The desired length of each combination.
40+
*/
3041
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
31-
if (current.size() == k) { // Base case: combination found
32-
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
42+
// Base case: combination found
43+
if (current.size() == k) {
44+
combinations.add(new ArrayList<>(current));
3345
return;
3446
}
3547

src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ void testCombinationThrows(int n, int k) {
2727

2828
private static Stream<Arguments> regularInputs() {
2929
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))),
30-
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))));
30+
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))),
31+
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))),
32+
Arguments.of(6, 4,
33+
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),
34+
List.of(1, 3, 4, 5), List.of(2, 3, 4, 5))));
3135
}
3236

3337
private static Stream<Arguments> wrongInputs() {
34-
return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100));
38+
return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100), Arguments.of(3, 4));
3539
}
3640
}

0 commit comments

Comments
 (0)