Finding all permutations to get the given sum Coin change problem
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The coin change problem has two similar-looking versions that developers often mix up: combinations and permutations. Combinations ignore order, while permutations treat different orderings as distinct results. If the task is to list every ordered way to reach a target sum, you need a backtracking approach that explores all choices at each remaining total.
Clarify the Problem First
For coins [1, 2] and target 3:
- combination results are
[1,1,1]and[1,2] - permutation results are
[1,1,1],[1,2], and[2,1]
That distinction changes both the algorithm and the output size. Permutations grow much faster because order matters.
Backtracking to List All Permutations
The most direct way to generate every ordered solution is recursion with a remaining sum and a current path.
This explores every valid ordering and stores each completed sequence.
Why This Finds Permutations
Notice that the loop always starts from the full coin list again. That means after choosing 2, the algorithm can still choose 1 next, which produces [2,1,...] separately from [1,2,...].
If you wanted combinations instead, you would carry a starting index and avoid revisiting earlier choices.
Avoiding Unnecessary Work
Sorting coins can make pruning easier, especially if you stop once a coin exceeds the remaining total.
This does not change correctness, but it reduces needless recursive calls.
Counting Without Listing
Sometimes you only need the number of permutations, not the actual lists. Then memoization is much cheaper.
This counts ordered solutions efficiently without building every path in memory.
Complexity Matters
Listing all permutations is inherently expensive because the output itself can be large. No algorithm can avoid that if you truly need every sequence.
That means you should ask early:
- Do I need all sequences?
- Or only the count?
- Or only one valid sequence?
Choosing the wrong version leads to unnecessary runtime and memory use.
Handling Duplicate Coin Values
If the input coin list itself contains duplicates, such as [1, 1, 2], the naive algorithm can generate duplicate outputs. In most coin-change problems, denominations are unique by definition, so normalize first.
This avoids duplicate branches caused by repeated equal denominations.
Practical Testing
Use small known examples:
Small fixtures make it obvious whether your code is counting combinations or permutations by accident.
Common Pitfalls
- Confusing permutations with combinations and using the wrong recursion strategy.
- Trying to list all solutions when only the count is needed.
- Forgetting to stop recursion when remaining total becomes negative.
- Allowing duplicate coin values in the input and generating duplicate outputs.
- Underestimating output size and running out of memory for larger targets.
Summary
- For permutation-style coin change, order matters and
[1,2]differs from[2,1]. - Backtracking is the clearest way to list every ordered solution.
- Memoization is better when you only need the count.
- Normalize coin denominations to avoid duplicate branches.
- Test with small examples to confirm you solved the right variant of the problem.

