-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinCoinChange.cpp
More file actions
40 lines (40 loc) · 919 Bytes
/
MinCoinChange.cpp
File metadata and controls
40 lines (40 loc) · 919 Bytes
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
#include <iostream>
#include <map>
#define ll long long int
using namespace std;
//Time Complexity is O(n*sum) and Space Complexity is O(n*sum).
ll minCoinChange(const int *ar,int n,int sum,map<int,ll> &mp){
if (sum==0){
return 0;
}
if (mp.count(sum)!=0){
return mp[sum];
}
mp[sum] = INT_MAX;
for (int i = 0; i < n; ++i) {
if (sum>=ar[i]) {
mp[sum] = min(mp[sum],1+minCoinChange(ar, n,sum - ar[i], mp));
}
}
return mp[sum];
}
int main(){
cout<<"Enter the value of n:\n";
int n;
cin>>n;
int *ar = new int [n];
cout<<"\nEnter the value of array:\n";
for (int i = 0; i < n; ++i) {
cin>>ar[i];
}
cout<<"\nEnter the sum to be searched :\n";
int sum;
cin>>sum;
map<int,ll> mp;
ll x = minCoinChange(ar,n,sum,mp);
if (x>=0){
cout<<x;
} else{
cout<<"Not Possible";
}
}