Skip to content

Commit 5603bf7

Browse files
committed
style: make SubsetCount a proper utility
1 parent cb401fe commit 5603bf7

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

spotbugs-exclude.xml

-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@
7474
<Match>
7575
<Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
7676
</Match>
77-
<Match>
78-
<Bug pattern="MS_SHOULD_BE_FINAL" />
79-
</Match>
8077
<Match>
8178
<Bug pattern="UWF_UNWRITTEN_FIELD" />
8279
</Match>

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
77
* @author Samrat Podder(https://github.com/samratpodder)
88
*/
9-
public class SubsetCount {
9+
public final class SubsetCount {
10+
private SubsetCount() {
11+
}
1012

1113
/**
1214
* Dynamic Programming Implementation.
@@ -16,7 +18,7 @@ public class SubsetCount {
1618
* @param target is the sum of each element of the subset taken together
1719
*
1820
*/
19-
public int getCount(int[] arr, int target) {
21+
public static int getCount(int[] arr, int target) {
2022
/**
2123
* Base Cases - If target becomes zero, we have reached the required sum for the subset
2224
* If we reach the end of the array arr then, either if target==arr[end], then we add one to
@@ -46,7 +48,7 @@ public int getCount(int[] arr, int target) {
4648
* @param arr is the input array on which subsets are to searched
4749
* @param target is the sum of each element of the subset taken together
4850
*/
49-
public int getCountSO(int[] arr, int target) {
51+
public static int getCountSO(int[] arr, int target) {
5052
int n = arr.length;
5153
int[] prev = new int[target + 1];
5254
prev[0] = 1;

src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@
55
import org.junit.jupiter.api.Test;
66

77
public class SubsetCountTest {
8-
public static SubsetCount obj = new SubsetCount();
9-
108
@Test
119
void hasMultipleSubset() {
1210
int[] arr = new int[] {1, 2, 3, 3};
13-
assertEquals(3, obj.getCount(arr, 6));
11+
assertEquals(3, SubsetCount.getCount(arr, 6));
1412
}
1513
@Test
1614
void singleElementSubset() {
1715
int[] arr = new int[] {1, 1, 1, 1};
18-
assertEquals(4, obj.getCount(arr, 1));
16+
assertEquals(4, SubsetCount.getCount(arr, 1));
1917
}
2018

2119
@Test
2220
void hasMultipleSubsetSO() {
2321
int[] arr = new int[] {1, 2, 3, 3};
24-
assertEquals(3, obj.getCountSO(arr, 6));
22+
assertEquals(3, SubsetCount.getCountSO(arr, 6));
2523
}
2624
@Test
2725
void singleSubsetSO() {
2826
int[] arr = new int[] {1, 1, 1, 1};
29-
assertEquals(1, obj.getCountSO(arr, 4));
27+
assertEquals(1, SubsetCount.getCountSO(arr, 4));
3028
}
3129
}

0 commit comments

Comments
 (0)