Skip to content

style: make SubsetCount a proper utility #5153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@
<Match>
<Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
</Match>
<Match>
<Bug pattern="MS_SHOULD_BE_FINAL" />
</Match>
<Match>
<Bug pattern="UWF_UNWRITTEN_FIELD" />
</Match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
* @author Samrat Podder(https://github.com/samratpodder)
*/
public class SubsetCount {
public final class SubsetCount {
private SubsetCount() {
}

/**
* Dynamic Programming Implementation.
Expand All @@ -16,7 +18,7 @@ public class SubsetCount {
* @param target is the sum of each element of the subset taken together
*
*/
public int getCount(int[] arr, int target) {
public static int getCount(int[] arr, int target) {
/**
* Base Cases - If target becomes zero, we have reached the required sum for the subset
* If we reach the end of the array arr then, either if target==arr[end], then we add one to
Expand Down Expand Up @@ -46,7 +48,7 @@ public int getCount(int[] arr, int target) {
* @param arr is the input array on which subsets are to searched
* @param target is the sum of each element of the subset taken together
*/
public int getCountSO(int[] arr, int target) {
public static int getCountSO(int[] arr, int target) {
int n = arr.length;
int[] prev = new int[target + 1];
prev[0] = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
import org.junit.jupiter.api.Test;

public class SubsetCountTest {
public static SubsetCount obj = new SubsetCount();

@Test
void hasMultipleSubset() {
int[] arr = new int[] {1, 2, 3, 3};
assertEquals(3, obj.getCount(arr, 6));
assertEquals(3, SubsetCount.getCount(arr, 6));
}
@Test
void singleElementSubset() {
int[] arr = new int[] {1, 1, 1, 1};
assertEquals(4, obj.getCount(arr, 1));
assertEquals(4, SubsetCount.getCount(arr, 1));
}

@Test
void hasMultipleSubsetSO() {
int[] arr = new int[] {1, 2, 3, 3};
assertEquals(3, obj.getCountSO(arr, 6));
assertEquals(3, SubsetCount.getCountSO(arr, 6));
}
@Test
void singleSubsetSO() {
int[] arr = new int[] {1, 1, 1, 1};
assertEquals(1, obj.getCountSO(arr, 4));
assertEquals(1, SubsetCount.getCountSO(arr, 4));
}
}