advanced9 min read·Updated October 5, 2026
Coin Change Combinations Explained: Making 5 with [1, 2, 5]
Master coin change combinations where order doesn't matter. Trace the DP table for [1, 2, 5] making 5, build mental models, and avoid counting permutations.
By Learnisim AI·Published October 5, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic 1D Dynamic Programming
- Nested loops
- Coin change permutations
Why
Why Order Doesn't Matter When Making Change
Phase 1: The Combinatorial Trap
Imagine you are building a cash register or automated vending system that needs to dispense change. You have coins of denominations and you need to make an exact target amount of 5. How many distinct ways can you form this total?
If you simply start listing every sequence of coin choices that adds up to 5, you will quickly run into a trap. For instance, is choosing a 2-cent coin followed by a 1-cent coin and another 2-cent coin different from choosing two 1-cent coins and then a 2-cent coin? If you treat them as separate events, you are counting permutations (ordered sequences).
Permutations treat , , and as three distinct transactions. But in currency, a handful of coins has no intrinsic order; holding two 2-cent coins and one 1-cent coin is a single unique physical combination. Without a strategy that enforces order-independence, your solution space explodes with redundant paths, making it impossible to scale to larger amounts.
Model
The Nested-Loop Architecture for Unordered Coin Combinations
Phase 2: The Canonical Loop Ordering Model
To count combinations for making 5 with coins = without double-counting permutations like and , we must restrict how coins are introduced. Imagine a single-row array
dp of size 6, where index stores the number of ways to form amount . We initialize dp[0] = 1 because there is exactly one way to make 0 (by picking no coins), and every other index starts at 0.The secret to ignoring order lies in the loop architecture. Instead of iterating amounts in the outer loop and coins in the inner loop (which generates ordered permutations), we iterate through each coin one by one in the outer loop, and sweep through amounts from
coin up to amount in the inner loop.By locking the coin loop to the outside, we force the algorithm to consider all ways to form amounts using only the first coin (1), then update the table to include the second coin (2), and finally the third coin (5). This enforces a strictly ascending order of coin inclusion, making every generated combination unique.
Worked example
Tracing the DP Table Step by Step for [1, 2, 5]
Phase 3: Worked example
To see the nested-loop architecture in action, let us trace the DP array for our locked example where
coins = [1, 2, 5] and amount = 5. We initialize an array of size 6 (indices 0 to 5) with zeros, except for our base case: dp[0] = 1. This base case signifies that there is exactly 1 way to make an amount of 0 (by using no coins at all).Step 1: Processing coin 1
When we processcoin = 1, our outer loop picks the coin, and our inner loop updates every amount from 1 to 5 using the recurrence relation .- For :
- For :
- For :
- For :
- For :
- For :
- For :
- For :
- For :
After processing coin 1, our array is
[1, 1, 1, 1, 1, 1]. Every amount from 0 to 5 has exactly 1 way to be formed (using only 1-cent coins).Step 2: Processing coin 2
Next, our outer loop moves tocoin = 2. The inner loop now runs from 2 to 5, adding to .- For : (representing combinations: and )
- For : (combinations: and )
- For : (combinations: , , and )
- For : (combinations: , , and )
- For : (combinations: and )
- For : (combinations: , , and )
- For : (combinations: , , and )
After processing coin 2, our array updates to
[1, 1, 2, 2, 3, 3].Step 3: Processing coin 5
Finally, our outer loop selectscoin = 5. The inner loop runs only for .- For : (adding the single coin to our existing combinations)
Result
The final DP array after all coins have been processed is[1, 1, 2, 2, 3, 4]. Looking at the final index, dp[5] = 4. These 4 valid, unordered combinations matching our expected result are , , , and .Practice
Predicting the DP Table for a Modified Coin Set
Phase 4: Practice
Now that you have traced the full progression for
amount = 5 with coins = [1, 2, 5], let us test your mental model on a constrained variant of the same problem. Suppose we keep the exact same coin set coins = [1, 2, 5], but reduce our target amount to 4.Recall our canonical state transition: keeping the outer loop over coins ensures each coin is processed in a fixed sequence, preventing permutations like and from being counted separately. When you evaluate
coins = [1, 2, 5] for an amount = 4, every intermediate cell in your dp array must reflect only unique combinations of those coins summing to that partial amount.The Challenge Task
What is the complete state of the
dp array of size 4 (indices 0 through 4) after processing all coins, and what final value represents the total number of combinations to make amount = 4?Work through the outer loop for coin
1, then coin 2, and finally coin 5, updating your array in place just as we did in the previous worked example. Keep strict track of how the inner loop bounds (x = coin to amount) prevent counting duplicate arrangements.Apply
Transfer: Scaling the Unordered Coin Pattern to New Constraints
Phase 5: Scaling the Pattern
You have seen how the canonical coin-outer loop prevents duplicate permutations when making amount 5 with coins . The power of this dynamic programming model lies in its invariance to scaling. When constraints shift—such as introducing quantity limits, alternate target amounts, or currency denominations with different GCD properties—the core principle remains unchanged: enforce a fixed processing order of available resources before accumulating target states.
Consider how you would adapt this technique if each coin could be used at most twice, or if you needed to find combinations for a larger target like amount 12. The outer-coin architecture ensures that you never revisit an earlier coin once its transitions for all valid amounts have been calculated. This guarantees time complexity while maintaining exact uniqueness.
When faced with a new variation, your first diagnostic step should be checking whether the problem asks for combinations or permutations. If order still does not count, keep the outer loop anchored to the coin universe and let the inner loop sweep the target interval.
FAQ
How many ways can you make 5 using the coin set [1, 2, 5] when order does not matter?
There are 4 combinations: [5], [2, 2, 1], [2, 1, 1, 1], and [1, 1, 1, 1, 1]. Permutations like [1, 4] or [2, 1, 2] are treated as identical because order is ignored.
Why does looping coins on the outside prevent duplicate permutations?
Iterating through coins in the outer loop forces the algorithm to consider coins in a strict, ascending sequence. This ensures that once coin
i is processed, we never look back at coin i-1, naturally eliminating duplicate orderings like [2, 1] after [1, 2].What is the time and space complexity of the unordered coin change DP solution?
The time complexity is O(n * amount), where n is the number of coins and amount is the target value. The space complexity is O(amount) since we only need a 1D DP array to track combination counts.
What is the main pitfall when transitioning from permutation coin change to combination coin change?
The most common mistake is nesting the coin loop inside the amount loop, which calculates permutations (order matters) instead of combinations. To fix this, swap the loops so the coin loop is on the outside.