Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C/C++ Program for Greedy Algorithm to find Minimum number of Coins
A greedy algorithm is an algorithmic approach used to find an optimal solution for the given problem. Greedy algorithm works by finding locally optimal solutions (optimal solution for a part of the problem) of each part so that the global optimal solution can be found.
In this problem, we will use a greedy algorithm to find the minimum number of coins/notes that could makeup to the given sum. For this we will take under consideration all the valid coins or notes i.e. denominations of { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 }. We need to return the number of these coins/notes required to make up the sum.
Let's take a few examples to understand the context better −
Example 1
Input : 1231 Output : 7
Explanation − We will need two Rs 500 notes, two Rs 100 notes, one Rs 20 note, one Rs 10 note and one Re 1 coin. That sums to 2+2+1+1+1 = 7 coins/notes.
Example 2
Input : 2150 Output : 3
Explanation − We will need one Rs 2000 note, one Rs 100 note, and one Rs 50 note.
To solve this problem using a greedy algorithm, we will find which is the largest denomination that can be used. Then we will subtract the largest denomination from the sum and repeat the same process until the sum becomes zero.
Syntax
void minchange(int sum)
{
// Find minimum coins needed for given sum
}
Algorithm
Input: sum Initialize coins = 0 Step 1: Find the largest denomination that can be used i.e. smaller than or equal to sum Step 2: Add denomination to coins array and subtract it from the sum Step 3: Repeat step 2 until the sum becomes 0 Step 4: Print each denomination used
Example
#include <stdio.h>
int notes[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 };
int n = 10; // Number of denominations
void minchange(int sum) {
int coins[100]; // Array to store coins used
int count = 0; // Number of coins used
for (int i = n - 1; i >= 0; i--) {
while (sum >= notes[i]) {
sum -= notes[i];
coins[count] = notes[i];
count++;
}
}
printf("Coins/Notes used: ");
for (int i = 0; i < count; i++) {
printf("%d ", coins[i]);
}
printf("\nTotal coins/notes needed: %d\n", count);
}
int main() {
int sum = 3253;
printf("The minimum number of coins/notes that sum up to %d:\n", sum);
minchange(sum);
return 0;
}
The minimum number of coins/notes that sum up to 3253: Coins/Notes used: 2000 500 500 200 50 2 1 Total coins/notes needed: 7
How It Works
- Start with the largest denomination (2000) and check if it's less than or equal to the sum
- If yes, subtract it from the sum and add it to the result
- Repeat until we can't use that denomination anymore
- Move to the next smaller denomination and repeat the process
- Continue until the sum becomes 0
Time Complexity
The time complexity is O(n) where n is the number of denominations, as we iterate through each denomination at most once.
Conclusion
The greedy approach for coin change works optimally for standard currency systems where larger denominations are multiples of smaller ones. It efficiently finds the minimum number of coins needed by always choosing the largest possible denomination at each step.
