-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change_using_function.cpp
More file actions
63 lines (52 loc) · 1.4 KB
/
coin_change_using_function.cpp
File metadata and controls
63 lines (52 loc) · 1.4 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
#include <bits/stdc++.h>
using namespace std;
#define limit 1000
long long n , coin[limit];
long long save[limit][limit];
long long value;
long long ways (long long index , long long amount)
{
/// base case----------------
if (index >= n){/// sob coin nea hoe gese
if (amount == value){ /// jodi coin gula dia value ta banano jay taile return 1
return 1;
}
else{ /// tasara return 0;
return 0;
}
}
/// dynamic programming memozition technique -------------
if (save[index][amount]!=-1){ /// save array te jodi kono valu thake taile valuta return korbo;
return save[index][amount]; //// aki calculation bar bar korar kono dorkar nai;
}
long long process1 = 0;
long long process2 = 0;
/// je je coin gula nibo segular jogfol kono somoyi value er besi houa jabe na
if (coin[index] + amount <= value)
{
process1 = ways (index , amount + coin[index]);
/// ekhane jehetu amra aki coin barbar use korte parbo
/// tai porer ta na nie amra age agerta niei try korbo
/// and protikhetre oi coin tar amount jog kore dibo
}
else{
process1 = 0;
/// otherwise process 0
}
process2 = ways (index + 1 , amount);
long long ans;
ans = process1 + process2;
save[index][amount] = ans;
return save[index][amount];
}
int main ()
{
memset (save , -1 , sizeof (save));
cin >> n;
for (int i = 0; i < n; i++){
cin >> coin[i];
}
cin >> value;
cout << ways (0 , 0) << endl;
main ();
}