We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b74243e commit f61e184Copy full SHA for f61e184
Dynamic Programming/2D/CountSubsetsWithSumK.java
@@ -0,0 +1,36 @@
1
+import java.util.*;
2
+import java.io.*;
3
+
4
+public class CountSubsetsWithSumK { //Assuming the elements of the array are positive integers
5
+ public static int findWays(int num[], int tar) {
6
+ int n = num.length;
7
+ int mod = 1000000007;
8
9
+ int[][] dp = new int[n][tar+1];
10
11
+ for(int i = 0; i<n; i++){
12
+ dp[i][0] = 1;
13
+ }
14
15
+ if(num[0]<= tar){
16
+ dp[0][num[0]] = 1;
17
18
19
+ if(num[0] == 0){
20
+ dp[0][0] = 2;
21
22
23
+ for(int i = 1; i<n; i++){
24
+ for(int j = 0; j<tar+1; j++){
25
+ int notPick = dp[i-1][j];
26
+ int pick = 0;
27
+ if(j>=num[i]){
28
+ pick = dp[i-1][j-num[i]];
29
30
+ dp[i][j] = (pick + notPick) % mod;
31
32
33
34
+ return dp[n-1][tar];
35
36
+}
0 commit comments