-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinChangeNotOptimized.cpp
125 lines (81 loc) · 1.85 KB
/
coinChangeNotOptimized.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<bits/stdc++.h>
using namespace std;
// denominations of coins
int countCoins(int total){
if(total < 1 )
return 0;
int coins = 0;
vector<int> vect{10,1};
int len = vect.size();
for(int i=0;i<len;i++){
if(total >= vect[i]){
coins += total/vect[i];
total %= vect[i];
}
if(total == 0)
break;
}
return coins;
}
int solveUtil(int denos[],int currIndex,int n,int sum){
if(sum == 0)
return 1;
if(sum < 0)
return 0;
if(currIndex == (n-1) && sum < denos[currIndex] && sum!=0)
return 0;
int ans = 0;
for(int i=currIndex;i<n;i++){
if(denos[i] <= sum){
ans += solveUtil(denos,i,n,sum-denos[i]);
}
}
return ans;
}
void solve(int denos[],int n,int sum){
/* int denos[] = {1,2,3};
int n = 3;
int sum = 4;*/
sort(denos,denos+n);
int cnt = solveUtil(denos,0,n,sum);
cout<<cnt<<endl;
}
// recursion solution for coin change
// Two possibilities for a given coin
// 1) Solution which contains the coin
// 2) Solution which does not contain the coin
// count the number of solutions for the given sum
int recurCoinChange(int denos[],int n,int sum){
if(sum == 0)
return 1;
if(sum<0)
return 0;
if(sum>=1 && n<=0)
return 0;
// don't add in the solution :+: add in a solution
return recurCoinChange(denos,n-1,sum) + recurCoinChange(denos,n,sum-denos[n-1]);
}
int main(){
int tc,n,sum;
cin>>tc;
while(tc--){
cin>>n;
int deons[n];
for(int i=0;i<n;i++)
cin>>deons[i];
cin>>sum;
// sort(deons,deons+n);
// solve(deons,n,sum);
cout<<recurCoinChange(deons,n,sum)<<endl;
}
return 0;
}
/*
2
3
1 2 3
4
4
2 5 3 6
10
*/