Skip to content

Commit 82354df

Browse files
author
alxkm
committed
refactor: SubsetSum
1 parent c57e02d commit 82354df

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,32 @@ private SubsetSum() {
55
}
66

77
/**
8-
* Driver Code
9-
*/
10-
public static void main(String[] args) {
11-
int[] arr = new int[] {50, 4, 10, 15, 34};
12-
assert subsetSum(arr, 64);
13-
/* 4 + 10 + 15 + 34 = 64 */
14-
assert subsetSum(arr, 99);
15-
/* 50 + 15 + 34 = 99 */
16-
assert !subsetSum(arr, 5);
17-
assert !subsetSum(arr, 66);
18-
}
19-
20-
/**
21-
* Test if a set of integers contains a subset that sum to a given integer.
8+
* Test if a set of integers contains a subset that sums to a given integer.
229
*
23-
* @param arr the array contains integers.
24-
* @param sum target sum of subset.
25-
* @return {@code true} if subset exists, otherwise {@code false}.
10+
* @param arr the array containing integers.
11+
* @param sum the target sum of the subset.
12+
* @return {@code true} if a subset exists that sums to the given value, otherwise {@code false}.
2613
*/
2714
public static boolean subsetSum(int[] arr, int sum) {
2815
int n = arr.length;
29-
boolean[][] isSum = new boolean[n + 2][sum + 1];
16+
boolean[][] isSum = new boolean[n + 1][sum + 1];
3017

31-
isSum[n + 1][0] = true;
32-
for (int i = 1; i <= sum; i++) {
33-
isSum[n + 1][i] = false;
18+
// Initialize the first column to true since a sum of 0 can always be achieved with an empty subset.
19+
for (int i = 0; i <= n; i++) {
20+
isSum[i][0] = true;
3421
}
3522

36-
for (int i = n; i > 0; i--) {
37-
isSum[i][0] = true;
38-
for (int j = 1; j <= arr[i - 1] - 1; j++) {
39-
if (j <= sum) {
40-
isSum[i][j] = isSum[i + 1][j];
23+
// Fill the subset sum matrix
24+
for (int i = 1; i <= n; i++) {
25+
for (int j = 1; j <= sum; j++) {
26+
if (arr[i - 1] <= j) {
27+
isSum[i][j] = isSum[i - 1][j] || isSum[i - 1][j - arr[i - 1]];
28+
} else {
29+
isSum[i][j] = isSum[i - 1][j];
4130
}
4231
}
43-
for (int j = arr[i - 1]; j <= sum; j++) {
44-
isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]);
45-
}
4632
}
4733

48-
return isSum[1][sum];
34+
return isSum[n][sum];
4935
}
5036
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
class SubsetSumTest {
10+
11+
record TestCase(int[] arr, int sum, boolean expected) {}
12+
13+
@ParameterizedTest
14+
@MethodSource("provideTestCases")
15+
void testSubsetSum(TestCase testCase) {
16+
assertEquals(testCase.expected(), SubsetSum.subsetSum(testCase.arr(), testCase.sum()));
17+
}
18+
19+
private static Stream<TestCase> provideTestCases() {
20+
return Stream.of(new TestCase(new int[] {50, 4, 10, 15, 34}, 64, true), new TestCase(new int[] {50, 4, 10, 15, 34}, 99, true), new TestCase(new int[] {50, 4, 10, 15, 34}, 5, false),
21+
new TestCase(new int[] {50, 4, 10, 15, 34}, 66, false), new TestCase(new int[] {}, 0, true), new TestCase(new int[] {1, 2, 3}, 6, true), new TestCase(new int[] {1, 2, 3}, 7, false), new TestCase(new int[] {3, 34, 4, 12, 5, 2}, 9, true));
22+
}
23+
}

0 commit comments

Comments
 (0)