Skip to content

Commit c848bab

Browse files
Related to #5164 (Redesign of ArrayCombination)
1 parent 37c2a96 commit c848bab

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.backtracking;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.TreeSet;
56

@@ -8,25 +9,37 @@
89
* @author TheClerici (<a href="https://github.com/TheClerici">git-TheClerici</a>)
910
*/
1011
public final class ArrayCombination {
11-
private ArrayCombination() {
12+
private ArrayCombination() {
1213
}
13-
private static int length;
1414

1515
/**
16-
* Find all combinations of 1..n by creating an array and using backtracking in Combination.java
17-
* @param n max value of the array.
18-
* @param k length of combination
19-
* @return a list of all combinations of length k. If k == 0, return null.
16+
* Find all combinations of 1..n using backtracking.
17+
*
18+
* @param n Max value of the elements.
19+
* @param k Length of the combination.
20+
* @return A list of all combinations of length k.
21+
* Returns an empty list if k is 0 or n is less than k.
2022
*/
21-
public static List<TreeSet<Integer>> combination(int n, int k) {
22-
if (n <= 0) {
23-
return null;
23+
public static List<List<Integer>> combination(int n, int k) {
24+
if (k <= 0 || n < k) {
25+
return new ArrayList<>(); // Return empty list for invalid input
2426
}
25-
length = k;
26-
Integer[] arr = new Integer[n];
27-
for (int i = 1; i <= n; i++) {
28-
arr[i - 1] = i;
27+
28+
List<List<Integer>> combinations = new ArrayList<>();
29+
combine(combinations, new ArrayList<>(), 1, n, k);
30+
return combinations;
31+
}
32+
33+
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
34+
if (current.size() == k) { // Base case: combination found
35+
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
36+
return;
37+
}
38+
39+
for (int i = start; i <= n; i++) {
40+
current.add(i);
41+
combine(combinations, current, i + 1, n, k); // Recursive call
42+
current.remove(current.size() - 1); // Backtrack
2943
}
30-
return Combination.combination(arr, length);
3144
}
3245
}
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
11
package com.thealgorithms.backtracking;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertNull;
53

4+
import java.util.ArrayList;
65
import java.util.List;
7-
import java.util.TreeSet;
8-
import org.junit.jupiter.api.Test;
6+
7+
import org.junit.jupiter.api.Test;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
public class ArrayCombinationTest {
1111

1212
@Test
13-
void testNBeingZeroOrLess() {
14-
List<TreeSet<Integer>> zeroResult = ArrayCombination.combination(0, 1);
15-
List<TreeSet<Integer>> negativeResult = ArrayCombination.combination(-1, 1);
16-
assertNull(zeroResult);
17-
assertNull(negativeResult);
13+
public void testCombination() {
14+
// Test case 1: n = 4, k = 2
15+
List<List<Integer>> expected = new ArrayList<>();
16+
expected.add(List.of(1, 2));
17+
expected.add(List.of(1, 3));
18+
expected.add(List.of(1, 4));
19+
expected.add(List.of(2, 3));
20+
expected.add(List.of(2, 4));
21+
expected.add(List.of(3, 4));
22+
23+
List<List<Integer>> actual = ArrayCombination.combination(4, 2);
24+
25+
assertEquals(expected, actual);
1826
}
1927

2028
@Test
21-
void testNoLengthElement() {
22-
List<TreeSet<Integer>> result = ArrayCombination.combination(2, 0);
23-
assertNull(result);
24-
}
29+
public void testEmptyCombination() {
30+
// Test case 2: n = 4, k = 0 (invalid input)
31+
List<List<Integer>> expected = new ArrayList<>();
32+
List<List<Integer>> actual = ArrayCombination.combination(4, 0);
2533

26-
@Test
27-
void testLengthOne() {
28-
List<TreeSet<Integer>> result = ArrayCombination.combination(2, 1);
29-
assert result != null;
30-
assertEquals(1, result.get(0).iterator().next());
31-
assertEquals(2, result.get(1).iterator().next());
34+
assertEquals(expected, actual);
3235
}
3336

3437
@Test
35-
void testLengthTwo() {
36-
List<TreeSet<Integer>> result = ArrayCombination.combination(2, 2);
37-
assert result != null;
38-
Integer[] arr = result.get(0).toArray(new Integer[2]);
39-
assertEquals(1, arr[0]);
40-
assertEquals(2, arr[1]);
41-
}
38+
public void testSingleElementCombinations() {
39+
// Test case 3: n = 3, k = 1
40+
List<List<Integer>> expected = new ArrayList<>();
41+
expected.add(List.of(1));
42+
expected.add(List.of(2));
43+
expected.add(List.of(3));
4244

43-
@Test
44-
void testLengthFive() {
45-
List<TreeSet<Integer>> result = ArrayCombination.combination(10, 5);
46-
assert result != null;
47-
Integer[] arr = result.get(0).toArray(new Integer[5]);
48-
assertEquals(1, arr[0]);
49-
assertEquals(5, arr[4]);
45+
List<List<Integer>> actual = ArrayCombination.combination(3, 1);
46+
47+
assertEquals(expected, actual);
5048
}
51-
}
49+
}

0 commit comments

Comments
 (0)