On this page
Say you have unlimited coins worth 1, 3 and 4 and must pay 6. Grabbing the biggest coin that fits gives 4 + 1 + 1, three coins. But 3 + 3 does it in two. That small example is why the coin change problem is taught with dynamic programming.
What is the coin change problem?
The change-making problem asks for the minimum number of coins of given denominations that add up to a given amount (Wikipedia: change-making problem). A second version asks how many ways the amount can be made. The problem is weakly NP-hard but can be solved optimally in pseudo-polynomial time by dynamic programming, so the work grows with the size of the amount.
Dynamic programming means building the answer for amount a from the answers for smaller amounts: you fill a table from 0 upwards and never solve the same amount twice.
Why does the greedy algorithm fail for coins 1, 3 and 4?
Greedy commits to the largest coin and never looks back. For 6 it takes 4, leaving 2, which needs 1 + 1. It never asks whether a smaller first coin leaves a better remainder.
This is not only a textbook trap. With India's old, now withdrawn, 5, 10, 20 and 25 paise coins, making 40 paise greedily gives 25 + 10 + 5, while 20 + 20 needs two coins (Wikipedia: change-making problem).
How do you fill the DP table for the fewest coins?
Let best[a] be the fewest coins that make a. Then best[0] = 0, and best[a] = 1 + the smallest best[a − c] over coins c no bigger than a. For coins 1, 3 and 4:
best[1] = 1 and best[2] = 2, using 1s
best[3] = 1 and best[4] = 1, a single coin each
best[5] = 2: from best[4], best[2] or best[1], the smallest is best[4], so 4 + 1
best[6] = 2: from best[5] = 2, best[3] = 1 or best[2] = 2, the smallest is best[3], so 3 + 3
At 6 the table compares all three possible last coins. Greedy only tried the 4.
How many ways can you make the amount?
Now count the ways to make 6 when order does not matter. Start with ways[0] = 1 and the rest 0. Bring in one coin at a time and, for each amount a from that coin upwards, add ways[a − coin] to ways[a]. For amounts 0 to 6:
Start: 1, 0, 0, 0, 0, 0, 0
After coin 1: 1, 1, 1, 1, 1, 1, 1
After coin 3: 1, 1, 1, 2, 2, 2, 3
After coin 4: 1, 1, 1, 2, 3, 3, 4
The four ways are 1+1+1+1+1+1, 3+1+1+1, 3+3 and 4+1+1. This is a restricted count of integer partitions, the subject of Ramanujan's partition congruences, checked by hand.
When is the greedy algorithm safe?
A coin system is called canonical if the greedy algorithm always gives the fewest coins (Wikipedia: change-making problem). Dexter Kozen and Shmuel Zaks proved that if coins 1 = c1 < c2 < ... < cm are not canonical, the smallest counterexample lies strictly between c3 + 1 and cm + cm−1 (Kozen and Zaks, Optimal bounds for the change-making problem). For coins 1, 3 and 4 that window holds only 6, exactly where greedy fails.
The Reserve Bank of India issues coins of 50 paise and ₹1, ₹2, ₹5, ₹10 and ₹20 (RBI FAQ on Indian currency). Four swaps, each cutting the coin count, show that ₹1, 2, 5 and 10 is canonical:
Two ₹1 coins become one ₹2.
Three ₹2 coins become ₹5 + ₹1.
Two ₹2 coins and a ₹1 become one ₹5.
Two ₹5 coins become one ₹10.
So a fewest-coin handful holds at most ₹9 below ₹10 (5 + 2 + 2), and must use as many ₹10 coins as fit, which is what greedy does; the argument repeats for ₹5 and ₹2. Adding ₹20 keeps it canonical: two ₹10 coins become one ₹20. Other greedy methods, like greedy colouring and Brooks' theorem, need their own proof of when they are safe.
A short practice plan
Write the recurrence in words before any code.
Fill the table by hand for a small amount, keeping the last coin.
Compare greedy with the table up to the sum of the two largest coins.
Decide whether the question counts combinations or orderings.
Try coins 1, 5, 6 and 9 with amount 11: greedy gives 9 + 1 + 1; the table should find 5 + 6.
If a step goes wrong, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



