Skip to content

Commit 3f0b835

Browse files
committed
Updated main and test
1 parent a216cb8 commit 3f0b835

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
public class SumOfSubset {
4+
5+
public static boolean subsetSum(int[] arr, int num, int Key) {
6+
if (Key == 0) {
7+
return true;
8+
}
9+
if (num < 0 || Key < 0) {
10+
return false;
11+
}
12+
13+
boolean include = subsetSum(arr, num - 1, Key - arr[num]);
14+
boolean exclude = subsetSum(arr, num - 1, Key);
15+
16+
return include || exclude;
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SumOfSubsetTest {
8+
9+
@Test
10+
void basicCheck() {
11+
assertEquals(true, SumOfSubset.subsetSum(new int[] {7, 3, 2, 5, 8}, 4, 14));
12+
13+
assertEquals(true, SumOfSubset.subsetSum(new int[] {4, 3, 2, 1}, 3, 5));
14+
15+
assertEquals(false, SumOfSubset.subsetSum(new int[] {1, 2, 7, 10, 9}, 4, 14));
16+
17+
assertEquals(false, SumOfSubset.subsetSum(new int[] {2, 15, 1, 6, 7}, 4, 4));
18+
19+
assertEquals(true, SumOfSubset.subsetSum(new int[] {1, 7, 2, 9, 10}, 4, 13));
20+
}
21+
}

0 commit comments

Comments
 (0)