Coin-change combination count: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Count unordered combinations using unlimited coins. Repeated denominations are counted once.

Process denominations outside the amount loop so each combination is counted without counting its ordering.

Open the 3D visualization

Coin-change combination count example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Coin-change combination count

Counting reusable-coin combinations.

Choosing an alternative

Minimum-coin DP optimizes count instead of counting solutions.

Worked example: input and state changes

Example input

1,2,5 | 5

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": []
    }
  2. Step 3: Include denomination 1: 1 ways to make 2.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        0,
        0,
        0
      ],
      "active": [
        2
      ]
    }
  3. Step 6: Include denomination 1: 1 ways to make 5.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        1,
        1,
        1
      ],
      "active": [
        5
      ]
    }
  4. Step 9: Include denomination 2: 3 ways to make 4.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        2,
        2,
        3,
        1
      ],
      "active": [
        4
      ]
    }
  5. Step 12: Coin-change combinations = 4.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        2,
        2,
        3,
        4
      ],
      "active": [
        5
      ]
    }

Expected final state

Coin-change combinations = 4.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(coins × amount).

Space complexity: O(amount) combination counts.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Coins (1–24 positive entries, 1–99) | amount (0–40).

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Changing loop order can count ordered sequences; total zero has one empty combination.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
7
8
9
// Positive denominations ≤ 99, n ≤ 24, amount ≤ 40. Returns unordered unlimited-coin combination count; duplicates are ignored. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, amount) {
    let dp = Array(256).fill(0); let seen = Array(256).fill(0); dp[0] = 1;
    for (let i = 0; i < n; i++) {
        let coin = a[i]; if (seen[coin] == 1) { continue; } seen[coin] = 1;
        for (let s = coin; s <= amount; s++) { dp[s] += dp[s - coin]; }
    }
    return dp[amount];
}

Source

Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.