C/C++ Program for Greedy Algorithm to find Minimum number of Coins

• Difficulty Level : Medium
• Last Updated : 02 Jan, 2019

Given a value V, if we want to make change for V Rs, and we have infinite supply of each of the denominations in Indian currency, i.e., we have infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, what is the minimum number of coins and/or notes needed to make the change?

Examples:

Input: V = 70
Output: 2
We need a 50 Rs note and a 20 Rs note.

Input: V = 121
Output: 3
We need a 100 Rs note, a 20 Rs note and a
1 Rs coin.

C/C++

 // C++ program to find minimum number of denominations#include using namespace std;  // All denominations of Indian Currencyint deno[] = { 1, 2, 5, 10, 20, 50, 100, 500, 1000 };int n = sizeof(deno) / sizeof(deno);  // Driver programvoid findMin(int V){    // Initialize result    vector ans;      // Traverse through all denomination    for (int i = n - 1; i >= 0; i--) {        // Find denominations        while (V >= deno[i]) {            V -= deno[i];            ans.push_back(deno[i]);        }    }      // Print result    for (int i = 0; i < ans.size(); i++)        cout << ans[i] << "  ";}  // Driver programint main(){    int n = 93;    cout << "Following is minimal number of change for " << n << " is ";    findMin(n);    return 0;}
Output:
Following is minimal number of change for 93 is 50  20  20  2  1

Please refer complete article on Greedy Algorithm to find Minimum number of Coins for more details!

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!
My Personal Notes arrow_drop_up