From cf6c87c35c309e754e6029e135c2801d143ff426 Mon Sep 17 00:00:00 2001
From: Piotr Idzik <65706193+vil02@users.noreply.github.com>
Date: Sat, 11 May 2024 16:36:17 +0200
Subject: [PATCH] style: make `SubsetCount` a proper utility (#5153)
---
spotbugs-exclude.xml | 3 ---
.../thealgorithms/dynamicprogramming/SubsetCount.java | 8 +++++---
.../dynamicprogramming/SubsetCountTest.java | 10 ++++------
3 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml
index 0e8a67c73..1f53feafb 100644
--- a/spotbugs-exclude.xml
+++ b/spotbugs-exclude.xml
@@ -74,9 +74,6 @@
-
-
-
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
index ef1c6c8f8..af31294f7 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
@@ -6,7 +6,9 @@ package com.thealgorithms.dynamicprogramming;
* 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.
@@ -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
@@ -46,7 +48,7 @@ public class SubsetCount {
* @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;
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
index 13f7b6f9c..c76f89deb 100644
--- a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
@@ -5,27 +5,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
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));
}
}