Skip to content

Commit e6f7063

Browse files
authored
Cleanup combination and combination test (TheAlgorithms#5902)
1 parent b5a097c commit e6f7063

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

33
import java.util.Arrays;
4+
import java.util.Collections;
45
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.TreeSet;
@@ -13,8 +14,6 @@ public final class Combination {
1314
private Combination() {
1415
}
1516

16-
private static int length;
17-
1817
/**
1918
* Find all combinations of given array using backtracking
2019
* @param arr the array.
@@ -23,39 +22,45 @@ private Combination() {
2322
* @return a list of all combinations of length n. If n == 0, return null.
2423
*/
2524
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
25+
if (n < 0) {
26+
throw new IllegalArgumentException("The combination length cannot be negative.");
27+
}
28+
2629
if (n == 0) {
27-
return null;
30+
return Collections.emptyList();
2831
}
29-
length = n;
3032
T[] array = arr.clone();
3133
Arrays.sort(array);
34+
3235
List<TreeSet<T>> result = new LinkedList<>();
33-
backtracking(array, 0, new TreeSet<T>(), result);
36+
backtracking(array, n, 0, new TreeSet<T>(), result);
3437
return result;
3538
}
3639

3740
/**
3841
* Backtrack all possible combinations of a given array
3942
* @param arr the array.
43+
* @param n length of the combination
4044
* @param index the starting index.
4145
* @param currSet set that tracks current combination
4246
* @param result the list contains all combination.
4347
* @param <T> the type of elements in the array.
4448
*/
45-
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
46-
if (index + length - currSet.size() > arr.length) {
49+
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
50+
if (index + n - currSet.size() > arr.length) {
4751
return;
4852
}
49-
if (length - 1 == currSet.size()) {
53+
if (currSet.size() == n - 1) {
5054
for (int i = index; i < arr.length; i++) {
5155
currSet.add(arr[i]);
52-
result.add((TreeSet<T>) currSet.clone());
56+
result.add(new TreeSet<>(currSet));
5357
currSet.remove(arr[i]);
5458
}
59+
return;
5560
}
5661
for (int i = index; i < arr.length; i++) {
5762
currSet.add(arr[i]);
58-
backtracking(arr, i + 1, currSet, result);
63+
backtracking(arr, n, i + 1, currSet, result);
5964
currSet.remove(arr[i]);
6065
}
6166
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
package com.thealgorithms.backtracking;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
36
import static org.junit.jupiter.api.Assertions.assertTrue;
47

58
import java.util.List;
9+
import java.util.Set;
610
import java.util.TreeSet;
711
import org.junit.jupiter.api.Test;
812

913
public class CombinationTest {
1014

15+
@Test
16+
void testNegativeElement() {
17+
Integer[] array = {1, 2};
18+
assertThrows(IllegalArgumentException.class, () -> { Combination.combination(array, -1); });
19+
}
20+
1121
@Test
1222
void testNoElement() {
1323
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 0);
14-
assertTrue(result == null);
24+
assertNotNull(result);
25+
assertEquals(0, result.size());
1526
}
1627

1728
@Test
@@ -28,4 +39,13 @@ void testLengthTwo() {
2839
assertTrue(arr[0] == 1);
2940
assertTrue(arr[1] == 2);
3041
}
42+
43+
@Test
44+
void testCombinationsWithStrings() {
45+
List<TreeSet<String>> result = Combination.combination(new String[] {"a", "b", "c"}, 2);
46+
assertEquals(3, result.size());
47+
assertTrue(result.contains(new TreeSet<>(Set.of("a", "b"))));
48+
assertTrue(result.contains(new TreeSet<>(Set.of("a", "c"))));
49+
assertTrue(result.contains(new TreeSet<>(Set.of("b", "c"))));
50+
}
3151
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy