Minimum Coins Problem in Data Structures and Algorithms
The Minimum Coins problem is a popular problem in Data Structures and Algorithms. We are given a collection of coin denominations and a target amount. Our task is to find the minimum number of coins required to make exactly that amount.
A common approach is to use a greedy algorithm. At each step, we select the largest coin denomination that does not exceed the remaining amount. However, this strategy does not always produce an optimal answer for every coin system.
1. What Is the Minimum Coins Problem?
Imagine that you have different types of coins and need to pay a particular amount of money. You want to use as few coins as possible.
For example, suppose the available coin denominations are:
Coins = {1, 2, 5, 10}
You need to make an amount of 18.
One possible combination is:
10 + 5 + 2 + 1 = 18
We used four coins. Our goal is to find a combination that uses the minimum possible number of coins.
Minimum number of coins = 4
2. Understanding Coin Denominations
A coin denomination represents the value of a particular coin. For example:
| Coin | Value |
|---|---|
| Coin 1 | 1 |
| Coin 2 | 2 |
| Coin 3 | 5 |
| Coin 4 | 10 |
If the target amount is 18, we can use different combinations of these denominations to make exactly 18.
3. What Is the Greedy Approach?
The greedy approach makes the best-looking choice at the current step without reconsidering previous choices.
In the standard greedy version of the Minimum Coins problem, we repeatedly choose the largest coin that is less than or equal to the remaining amount.
Greedy Strategy
- Sort the coin denominations in descending order.
- Start with the complete target amount.
- Choose the largest coin that does not exceed the remaining amount.
- Subtract that coin's value from the remaining amount.
- Repeat until the remaining amount becomes zero or no coin can be selected.
4. Example of the Greedy Approach
Suppose we are given:
Coins = {1, 2, 5, 10}
Target = 18
We start by selecting the largest coin that does not exceed 18.
Step 1: Select Coin 10
Remaining amount = 18 - 10
= 8
Step 2: Select Coin 5
Remaining amount = 8 - 5
= 3
Step 3: Select Coin 2
Remaining amount = 3 - 2
= 1
Step 4: Select Coin 1
Remaining amount = 1 - 1
= 0
Final Result
Coins used = 10 + 5 + 2 + 1
Minimum number of coins = 4
5. Dry Run of the Greedy Algorithm
Let's understand the complete process using a table.
Coins = {1, 2, 5, 10}
Target = 18
| Step | Remaining Amount | Selected Coin | New Remaining Amount |
|---|---|---|---|
| 1 | 18 | 10 | 8 |
| 2 | 8 | 5 | 3 |
| 3 | 3 | 2 | 1 |
| 4 | 1 | 1 | 0 |
The remaining amount becomes zero after selecting four coins.
6. C++ Implementation Using the Greedy Approach
We can implement the greedy algorithm by sorting the coin denominations in descending order and selecting the largest possible coin at every step.
C++ Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minimumCoins(vector<int>& coins, int amount) {
// Sort coins in descending order
sort(coins.rbegin(), coins.rend());
int count = 0;
for (int coin : coins) {
// Select the coin as many times as possible
if (coin <= amount) {
int number = amount / coin;
count += number;
amount %= coin;
}
}
// Check whether the amount can be formed
if (amount != 0) {
return -1;
}
return count;
}
int main() {
vector<int> coins = {1, 2, 5, 10};
int amount = 18;
int answer = minimumCoins(coins, amount);
if (answer == -1) {
cout << "Amount cannot be formed";
}
else {
cout << "Minimum number of coins = "
<< answer;
}
return 0;
}
Output
Minimum number of coins = 4
7. Explanation of the C++ Code
1. Sorting the Coins
sort(coins.rbegin(), coins.rend());
This sorts the coin denominations in descending order. The largest coin appears first.
2. Initializing the Count
int count = 0;
This variable stores the total number of coins selected.
3. Iterating Through the Coins
for (int coin : coins)
We process each coin denomination from largest to smallest.
4. Finding How Many Coins Can Be Used
int number = amount / coin;
Integer division tells us the maximum number of coins of this denomination that can be selected without exceeding the remaining amount.
5. Updating the Coin Count
count += number;
We add the selected number of coins to the total count.
6. Updating the Remaining Amount
amount %= coin;
The modulo operator gives the remaining amount after using as many coins of the current denomination as possible.
7. Checking Whether the Amount Can Be Formed
if (amount != 0) {
return -1;
}
If the remaining amount is not zero after processing all denominations, the target amount cannot be formed using the available coins.
8. Does Greedy Always Give the Minimum Number of Coins?
No. The greedy approach does not always produce the optimal answer for every possible set of coin denominations.
It works for many common currency systems, but some coin systems require a different approach.
Example Where Greedy Fails
Coins = {1, 3, 4}
Target = 6
The greedy algorithm selects the largest coin first.
Greedy Selection
- Select coin 4. Remaining amount = 2.
- Select coin 1. Remaining amount = 1.
- Select coin 1. Remaining amount = 0.
Greedy result = 4 + 1 + 1
Number of coins = 3
Optimal Selection
Instead of selecting coin 4 first, select two coins of value 3.
3 + 3 = 6
Optimal number of coins = 2
9. Minimum Coins Using Dynamic Programming
Dynamic Programming is a reliable approach for finding the minimum number of coins when arbitrary positive coin denominations are given.
Instead of making only one greedy choice, we consider the possible choices for every smaller amount and store the best answers.
DP State
dp[i] = Minimum number of coins required
to make amount i
Base Case
dp[0] = 0
We need zero coins to make an amount of zero.
Transition
For every amount, try every coin that does not exceed it.
dp[i] = min(dp[i], 1 + dp[i - coin])
We add one coin to the best solution for the remaining amount.
C++ Code Using Dynamic Programming
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minimumCoinsDP(
vector<int>& coins,
int amount
) {
const int INF = amount + 1;
vector<int> dp(amount + 1, INF);
// Base case
dp[0] = 0;
// Calculate answers for every amount
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (coin <= i) {
dp[i] = min(
dp[i],
1 + dp[i - coin]
);
}
}
}
// Check whether the target is possible
if (dp[amount] == INF) {
return -1;
}
return dp[amount];
}
int main() {
vector<int> coins = {1, 3, 4};
int amount = 6;
cout << "Minimum number of coins = "
<< minimumCoinsDP(coins, amount);
return 0;
}
Output
Minimum number of coins = 2
10. Dry Run of the Dynamic Programming Approach
Consider:
Coins = {1, 3, 4}
Target = 6
Initially, all DP values are set to infinity except dp[0] = 0.
| Amount | Minimum Coins | One Optimal Combination |
|---|---|---|
| 0 | 0 | No coins |
| 1 | 1 | 1 |
| 2 | 2 | 1 + 1 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 4 + 1 |
| 6 | 2 | 3 + 3 |
The final answer is dp[6] = 2 because two coins of value 3 are sufficient to make the target amount.
11. Greedy vs Dynamic Programming for Minimum Coins
| Feature | Greedy Approach | Dynamic Programming |
|---|---|---|
| Decision | Chooses the largest available coin first. | Considers all valid coin choices. |
| Optimality | Not guaranteed for every coin system. | Finds the optimal answer for positive denominations. |
| Time Complexity | O(n log n) for sorting plus O(n) processing. | O(amount × n). |
| Space Complexity | O(1) auxiliary space apart from sorting details. | O(amount). |
| Best Use | Coin systems where greedy choices are known to be optimal. | Arbitrary positive coin denominations. |
12. Time and Space Complexity
Greedy Approach
Let n be the number of different coin denominations.
- Sorting the coins: O(n log n).
- Traversing the denominations: O(n).
- Using division and modulo: O(1) per denomination in the usual word-RAM model.
Overall Time Complexity = O(n log n)
The algorithm uses O(1) auxiliary space apart from sorting implementation details.
Dynamic Programming Approach
Let A be the target amount and n be the number of coin denominations.
- We calculate answers for A different positive amounts.
- For each amount, we check up to n coin denominations.
Time Complexity = O(A × n)
Space Complexity = O(A)
13. Applications of the Minimum Coins Problem
- Currency Systems: Finding a small number of currency notes or coins to make a particular amount.
- Payment Systems: Selecting denominations to complete a payment.
- Vending Machines: Determining how to return change using available denominations.
- Resource Allocation: Selecting units of different sizes to reach a required total.
- Algorithm Design: Understanding the difference between greedy choices and globally optimal solutions.
14. Common Mistakes to Avoid
- Assuming greedy always works: It can fail for some coin systems, such as {1, 3, 4} with target 6.
- Sorting in ascending order: The greedy approach should process the largest denomination first.
- Forgetting to update the amount: After selecting coins, update the remaining amount correctly.
- Ignoring impossible amounts: Check whether the remaining amount can be formed.
- Incorrect DP initialization: Set dp[0] to zero and all other states to an unreachable value initially.
- Using zero or negative denominations: Standard coin-change algorithms assume positive coin values.
15. Interview Questions on Minimum Coins
Q1. What is the Minimum Coins problem?
It asks us to find the minimum number of available coins required to form a given target amount.
Q2. Which technique is used in the greedy version?
It uses a greedy strategy that selects the largest possible denomination at every step.
Q3. Does greedy always give the optimal answer?
No. Greedy is optimal only for suitable coin systems. For arbitrary denominations, it can return a non-optimal result.
Q4. Which approach guarantees the minimum number of coins for arbitrary positive denominations?
Dynamic Programming can find the optimal answer by considering all valid coin choices for each smaller amount.
Q5. What is the time complexity of the DP solution?
It is O(A × n), where A is the target amount and n is the number of coin denominations.
Q6. What is the base case in the DP solution?
dp[0] = 0 because no coins are needed to make an amount of zero.
Q7. What happens if the amount cannot be formed?
The algorithm returns -1 to indicate that no valid combination of the available coins can form the target.
16. Practice Problems
- Given coins {1, 2, 5, 10} and target 27, find the minimum number of coins using the greedy approach.
- Given coins {1, 3, 4} and target 6, compare the greedy and optimal answers.
- Given coins {1, 5, 10, 25} and target 63, find the number of coins selected by the greedy algorithm.
- Write a C++ program to solve the Minimum Coins problem using Dynamic Programming.
- Explain why the greedy approach fails for some coin systems.
- Given coins {2, 5, 7} and target 11, find the minimum number of coins.
17. Quick Revision
- The Minimum Coins problem asks for the fewest coins needed to make a target amount.
- The greedy approach selects the largest possible coin first.
- Greedy is not optimal for every possible coin system.
- For arbitrary positive denominations, Dynamic Programming can find the optimal answer.
- The DP state dp[i] stores the minimum coins needed to form amount i.
- The base case is dp[0] = 0.
- Greedy sorting takes O(n log n) time.
- Dynamic Programming takes O(A × n) time and O(A) space.