forked from theutpal01/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfectsum.cpp
84 lines (58 loc) · 1.52 KB
/
perfectsum.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int perfectSum(int arr[], int n, int sum)
{
// // Your code goes here
int t[n+1][sum+1] ={0};
// int c=0;
// for(int i=0;i<n;i++) if(!arr[i]) c++;
// for(int j=1;j<sum+1;j++) t[0][j]=0;
// for(int i=0;i<n+1;i++)t[i][0] = 1;
for(int i = 0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
t[i][j] = 0;
if(j==0)
t[i][j] = 1;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(arr[i-1]<=j)
t[i][j] = (t[i-1][j-arr[i-1]]%1000000007+t[i-1][j]%1000000007)%1000000007;
else
t[i][j] = t[i-1][j]%1000000007;
}
}
return t[n][sum];
//vector<vector<int>> dp(n+2,vector<int>(sum+2,-1));
//int ans = count(sum,arr,0,&dp,n);
// return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n, sum;
cin >> n >> sum;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
Solution ob;
cout << ob.perfectSum(a, n, sum) << "\n";
}
return 0;
}
// } Driver Code Ends