Repository: TheAlgorithms/Java
Pull request #3580 (2022-10-17 21:34:57)

DP Solution to Subset Count
### **Describe your change:**

- [x] Add an algorithm?

#### References

Contribution with reference to Issue #3553 

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
new file mode 100644
index 00000000..2d36f5ad
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
@@ -0,0 +1,69 @@
+package com.thealgorithms.dynamicprogramming;
+
+/**
+ * Find the number of subsets present in the given array with a sum equal to target.
+ * Based on Solution discussed on 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 {
+
+
+    /**
+     * Dynamic Programming Implementation.
+     * Method to find out the number of subsets present in the given array with a sum equal to target.
+     * Time Complexity is O(n*target) and Space Complexity is O(n*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 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 the final count
+         * Otherwise we add 0 to the final count
+         */
+        int n = arr.length;
+        int[][] dp = new int[n][target+1];
+        for (int i = 0; i < n; i++) {
+            dp[i][0] = 1;
+        }
+        if(arr[0]<=target) dp[0][arr[0]] = 1;
+        for(int t=1;t<=target;t++){
+            for (int idx = 1; idx < n; idx++) {
+                int notpick = dp[idx-1][t];
+                int pick =0;
+                if(arr[idx]<=t) pick+=dp[idx-1][target-t];
+                dp[idx][target] = pick+notpick;
+            }
+        }
+        return dp[n-1][target];
+    }
+
+
+    /**
+     * This Method is a Space Optimized version of the getCount(int[], int) method and solves the same problem
+     * This approach is a bit better in terms of Space Used
+     * Time Complexity is O(n*target) and Space Complexity is O(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){
+        int n = arr.length;
+        int prev[]=new int[target+1];
+        prev[0] =1;
+        if(arr[0]<=target) prev[arr[0]] = 1;
+        for(int ind = 1; ind<n; ind++){
+            int cur[]=new int[target+1];
+            cur[0]=1;
+            for(int t= 1; t<=target; t++){
+                int notTaken = prev[t];
+                int taken = 0;
+                if(arr[ind]<=t) taken = prev[t-arr[ind]];
+                cur[t]= notTaken + taken;
+            }
+            prev = cur;
+        }
+        return prev[target];
+    }
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
new file mode 100644
index 00000000..5a532067
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.dynamicprogramming;
+
+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));
+    }
+    @Test
+    void singleElementSubset(){
+        int[] arr = new int[]{1,1,1,1};
+        assertEquals(4, obj.getCount(arr, 1));
+    }
+
+    @Test
+    void hasMultipleSubsetSO(){
+        int[] arr = new int[]{1,2,3,3};
+        assertEquals(3, obj.getCountSO(arr, 6));
+    }
+    @Test
+    void singleSubsetSO(){
+        int[] arr = new int[]{1,1,1,1};
+        assertEquals(1,obj.getCountSO(arr, 4));
+    }
+}