Algorithms · Visualized

Coin Change (Greedy)

Greedy

Make the locally optimal choice at each step, hoping it leads to a globally optimal solution.

To make a target amount using the fewest coins, the greedy approach always picks the largest coin denomination that doesn't exceed the remaining amount. Time: O(n) — n distinct denominations average, O(n) worst; space: O(1).

Big-O: Works when the problem has optimal substructure + greedy-choice property — e.g. intervals, Huffman, fractional knapsack.

Coin Change (Greedy)

Greedy

To make a target amount using the fewest coins, the greedy approach always picks the largest coin denomination that doesn't exceed the remaining amount. This works correctly for 'canonical' coin systems (like standard currency: 1, 5, 10, 25) but can fail to find the true minimum for arbitrary denominations — which is why the general Coin Change problem uses dynamic programming instead.

How Coin Change (Greedy) Works

  1. Sort the available coin denominations in descending order.
  2. Starting with the largest coin, use as many of that coin as possible without exceeding the remaining amount.
  3. Subtract the value used from the remaining amount, and record how many of that coin were used.
  4. Move to the next smaller denomination and repeat the process.
  5. Continue until the remaining amount reaches 0 — the collected coins are the greedy solution.

Coin Change (Greedy) Worked Example

Input: {amount: 63, coins: [25, 10, 5, 1]}

Use 25s: 63/25=2 with remainder 13 → use two 25s. Use 10s: 13/10=1, remainder 3 → use one 10. Use 5s: 3/5=0, skip. Use 1s: 3/1=3 → use three 1s. Total coins used: 2+1+0+3 = 6 coins (25,25,10,1,1,1).

Coin Change (Greedy)O(n)
Always take the largest coin ≤ remaining. Optimal for canonical coin sets.
25
0
10
1
5
2
1
3
Step 1 / 6
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Coin Change (greedy) for amount 63 with coins [25, 10, 5, 1].
Python · Greedy Algorithms in Code
def coin_change_greedy(coins, amount):
    coins.sort(reverse=True)
    count = 0
    for c in coins:
        count += amount // c
        amount %= c
    return count if amount == 0 else -1

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Coin Change (Greedy) Time & Space Complexity

BestO(amount / largest coin)
AverageO(n) — n distinct denominations
WorstO(n)
SpaceO(1)

When to Use Coin Change (Greedy)

  • Everyday cash-register change-making with standard currency denominations.
  • A cautionary example showing when greedy algorithms give the correct answer versus when full DP is required (arbitrary coin sets can break the greedy approach).

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.