Skip to content

Commit e08684c

Browse files
committed
Added: Algorithm to find minimum number of denominations
1 parent 272d442 commit e08684c

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

algorithms/greedy/minimumCoins.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Given a value V and the list of available denomination of money,
2+
// find minimum number of coins and/or notes needed to make the change.
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// All denominations of Indian Currency
8+
int deno[] = { 1, 2, 5, 10, 20,
9+
50, 100, 500, 1000 };
10+
int n = sizeof(deno) / sizeof(deno[0]);
11+
12+
vector<int> calculate(int V)
13+
{
14+
sort(deno, deno + n);
15+
vector<int> ans;
16+
17+
for (int i = n - 1; i >= 0; i--) {
18+
19+
while (V >= deno[i]) {
20+
V -= deno[i];
21+
ans.push_back(deno[i]);
22+
}
23+
}
24+
return ans;
25+
//for (int i = 0; i < ans.size(); i++)
26+
//cout << ans[i] << " ";
27+
}
28+
29+
int main()
30+
{
31+
int n;
32+
cout<<"Enter the monitory value: ";
33+
cin>>n;
34+
cout << "Following is minimal number of change for " << n
35+
<< ": ";
36+
vector<int> ans = calculate(n);
37+
for(auto i: ans)
38+
cout<<i<<" ";
39+
cout<<"\nMinimum Denominations required: "<<ans.size();
40+
return 0;
41+
}

0 commit comments

Comments
 (0)