-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSubsetSum.java
More file actions
65 lines (50 loc) · 1.54 KB
/
Copy pathCountSubsetSum.java
File metadata and controls
65 lines (50 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# PROBLEM : [ https://practice.geeksforgeeks.org/problems/perfect-sum-problem5633/1# ]
# Solution :
// { Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GfG
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int sum = sc.nextInt();
int arr[] = new int[n];
for(int i = 0;i<n;i++)
arr[i] = sc.nextInt();
Solution ob = new Solution();
System.out.println(ob.perfectSum(arr,n,sum));
}
}
} // } Driver Code Ends
class Solution{
public int perfectSum(int arr[],int n, int sum)
{
if(n==0){
return 0;
}
int t[][] = new int[n+1][sum+1];
for(int i=0;i<sum+1;i++){
t[0][i] = 0;
}
for(int i=0;i<n+1;i++){
t[i][0] = 1;
}
for(int i=1;i<n+1;i++){
for(int j=1;j<sum+1;j++){
if(arr[i-1]<=j){
t[i][j] = (t[i-1][j] + t[i-1][j-arr[i-1]])%1000000007;
}else{
t[i][j] = t[i-1][j]%1000000007;
}
}
}
return t[n][sum]%1000000007;
}
}
# Explaination : [ https://www.geeksforgeeks.org/count-of-subsets-with-sum-equal-to-x/ ]