Coin Change Counting: Why Loop Order Changes the Answer
By Hourly DSA · · 4 min read
To count coin-change combinations, process one denomination at a time and update amounts in ascending order. This lets you reuse the current coin while counting each multiset of coins once. Reversing which loop comes first can count ordered sequences instead.
Assume distinct positive integer denominations, unlimited copies of each, and a nonnegative integer target. We are counting ways to make the target, not finding the fewest coins.
Decide whether order creates a new answer
With denominations [1, 2] and target 4, there are three combinations:
1 + 1 + 1 + 1
1 + 1 + 2
2 + 2
The arrangements 1 + 2 + 1 and 2 + 1 + 1 represent the same combination as 1 + 1 + 2.
If order does matter, there are five sequences: the all-ones sequence, the three placements of one 2 among two ones, and 2 + 2. Both counts can be correct, but they answer different questions.
Define the state after each coin
Let ways[a] count combinations totaling amount a using only denominations processed so far.
Initialize ways[0] = 1 and every other entry to zero. There is one way to make zero: choose no coins. That empty choice is what lets the first usable coin create a combination.
ways = array of target + 1 zeros
ways[0] = 1
for coin in denominations:
for amount from coin through target:
ways[amount] += ways[amount - coin]
return ways[target]
This is an in-place tabulation. Its meaning depends on both the stored values and the order in which updates occur.
Trace the table for target four
Initially, the entries for amounts 0 through 4 are [1, 0, 0, 0, 0].
After processing coin 1, they are [1, 1, 1, 1, 1]. Every amount has exactly one combination using only ones.
Now process coin 2. At amount 2, add ways[0], giving two combinations: two ones or one two. At amount 3, add ways[1], giving two combinations. At amount 4, add the already updated ways[2], raising the count from 1 to 3.
That last update includes two copies of coin 2. Ascending amounts allow the current denomination to be reused within its own pass.
Why doesn't it count rearrangements?
Before processing a coin, an entry counts combinations without that denomination. During the pass, adding ways[amount - coin] contributes combinations that use at least one copy of it.
Removing one current coin gives a unique smaller combination represented by that source entry. Adding the coin back reverses the operation. The combinations with no current coin and those with at least one are separate, so adding their counts is valid.
Because denominations are introduced in a fixed order, the table never creates separate histories for placing the same coins in different orders. The denominations need not be numerically sorted; they just need to be distinct and processed consistently.
What happens when amounts come first?
If the outer loop visits amounts and the inner loop tries every coin, the state instead counts sequences by their final coin. For amount 4, append 1 to each sequence totaling 3, or append 2 to each sequence totaling 2.
There are three sequences totaling 3 and two totaling 2, so that recurrence returns 5.
Changing to descending amounts inside the coin-first version has another meaning: it prevents reusing that denomination during its pass, modeling at most one copy of each listed coin.
Loop order is part of the recurrence, not a formatting choice.
Check the assumptions and cost
For k denominations and target T, the combination algorithm takes O(kT) time and O(T) space under fixed-cost arithmetic. Counts may become large, so choose a representation that can hold them or use a modulus if the problem specifies one.
Duplicate denominations would count the same coin type more than once. Zero or negative denominations break the positive-amount dependency used here. Validate those conditions before applying the recurrence.
With no denominations, target zero returns 1 and any positive target returns 0.
Try one smaller target
For coins [1, 3] and target 4, how many combinations exist?
Two: four ones, or one 1 and one 3. Counting sequences would give three because 1 + 3 and 3 + 1 would be separate.
Use Hourly DSA to practice defining what one table entry counts before writing its update.
Read next
Build the habit that makes loop-order decisions easier to explain.