C/C++ Program for Greedy Algorithm to find Minimum number of Coins
Write a C/C++ program for a given value of V Rs and an infinite supply of each of the denominations {1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, The task is to find the minimum number of coins and/or notes needed to make the change?
Examples:
Input: V = 70
Output: 2
Explanation: We need a 50 Rs note and a 20 Rs note.Input: V = 121
Output: 3
Explanation: We need a 100 Rs note, a 20 Rs note, and a 1 Rs coin.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
Approach:
The intuition would be to take coins with greater value first. This can reduce the total number of coins needed. Start from the largest possible denomination and keep adding denominations while the remaining value is greater than 0.
Step-by-step approach:
- Sort the array of coins in decreasing order.
- Initialize ans vector as empty.
- Find the largest denomination that is smaller than remaining amount and while it is smaller than the remaining amount:
- Add found denomination to ans. Subtract value of found denomination from amount.
- If amount becomes 0, then print ans.
Below is the implementation of the above approach:
C++
// C++ program to find minimum // number of denominations #include <bits/stdc++.h> using namespace std; // All denominations of Indian Currency int denomination[] = { 1, 2, 5, 10, 20, 50, 100, 500, 1000 }; int n = sizeof (denomination) / sizeof (denomination[0]); void findMin( int V) { sort(denomination, denomination + n); // Initialize result vector< int > ans; // Traverse through all denomination for ( int i = n - 1; i >= 0; i--) { // Find denominations while (V >= denomination[i]) { V -= denomination[i]; ans.push_back(denomination[i]); } } // Print result for ( int i = 0; i < ans.size(); i++) cout << ans[i] << " " ; } // Driver Code int main() { int n = 93; cout << "Following is minimal" << " number of change for " << n << ": " ; // Function Call findMin(n); return 0; } |
Following is minimal number of change for 93: 50 20 20 2 1
Time Complexity: O(V).
Auxiliary Space: O(V).
Note: The above approach may not work for all denominations.
For example, it doesn’t work for denominations {9, 6, 5, 1} and V = 11. The above approach would print 9, 1 and 1. But we can use 2 denominations 5 and 6.
For general input, below dynamic programming approach can be used: Find minimum number of coins that make a given value.
C/C++ Program for Greedy Algorithm to find Minimum number of Coins using Ladder If-Else approach:
we will simply iterate through the greater to smaller coins until the n is greater to that coin and decrement that value from n afterward using ladder if-else and will push back that coin value in the vector.
Step-by-step approach:
- Declare a vector that store the coins.
- while n is greater than 0 iterate through greater to smaller coins:
- if n is greater than equal to 2000 than push 2000 into the vector and decrement its value from n.
- else if n is greater than equal to 500 than push 500 into the vector and decrement its value from n.
- And so on till the last coin using ladder if else.
- return the vector/array
Below is the Implementation of the above approach:
C++
// C++ program to find minimum // number of coins #include <bits/stdc++.h> using namespace std; vector< int > findMin( int n) { // initialize vector to store the coins vector< int > v; // iterate till n>0 and check condition according to the // greatest coin possible while (n > 0) { if (n >= 2000) { v.push_back(2000); n -= 2000; } else if (n >= 500) { v.push_back(500); n -= 500; } else if (n >= 200) { v.push_back(200); n -= 200; } else if (n >= 100) { v.push_back(100); n -= 100; } else if (n >= 50) { v.push_back(50); n -= 50; } else if (n >= 20) { v.push_back(20); n -= 20; } else if (n >= 10) { v.push_back(10); n -= 10; } else if (n >= 5) { v.push_back(5); n -= 5; } else if (n >= 2) { v.push_back(2); n -= 2; } else if (n >= 1) { v.push_back(1); n -= 1; } } // return the ans that stores in the vector return v; } // Driver Code int main() { int v = 93; cout << "Following is minimal" << " number of change for " << v << ": " ; // Function Call vector< int > vec = findMin(v); // print the vector for ( auto it : vec) cout << it << " " ; return 0; } // this code is contributed by Prateek Kumar Singh |
Following is minimal number of change for 93: 50 20 20 2 1
Time Complexity: O(N) that is equal to the amount v.
Auxiliary Space: O(1) that is optimized
Please refer complete article on Greedy Algorithm to find Minimum number of Coins for more details!