intermediate8 min read·Updated October 4, 2026

Coin Change Explained: Min Coins for Amount 6 with [1, 3, 4]

Master the Coin Change DP problem by tracing amount 6 with coins [1, 3, 4]. Learn why greedy fails, build mental models, and handle edge cases like -1.

By Learnisim AI·Published October 4, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • basic recursion
  • 1D array manipulation
  • memoization or tabulation fundamentals
Coin Change (Min Coins) — Target Amount: 6, Coins: [1, 3, 4] Greedy fails because largest-first takes 3 coins (4+1+1), while DP finds the optimal 2 coins (3+3) Phase 1: The Greedy Trap (Largest Coin First) 4 rem: 2 1 rem: 1 1 rem: 0 3 Coins Suboptimal Result! Greedy: 4 + 1 + 1 = 3 coins Phase 2: The Optimal Solution (Dynamic Programming) 3 rem: 3 3 rem: 0 2 Coins (Optimal!) Optimal: 3 + 3 = 2 coins Phase 3: DP Table Construction — dp[x] = min(dp[x], dp[x - coin] + 1) Indices represent amounts 0 through 6. Values represent minimum coins needed. Amount 0 0 Base Case Amount 1 1 dp[0]+1 (c=1) Amount 2 2 dp[1]+1 (c=1) Amount 3 1 min(3, dp[0]+1) Amount 4 1 dp[0]+1 (c=4) Amount 5 2 dp[2]+1 (c=3) Amount 6 2 dp[3]+1 (c=3) Optimal substructure: dp[6] = dp[6-3] + 1 = dp[3] + 1 = 2
Min coins for amount 6 with coins [1, 3, 4] overview diagram
Why

Why Greedy Fails: Making Change with Coins [1, 3, 4]

Phase 1: The Trap of the Largest Coin

Imagine you are at a cash register with an unlimited supply of coins in denominations , and your target amount is 6. Your goal is simple: find the absolute fewest number of coins needed to sum up to 6.
Your first instinct is likely the greedy approach—grab the largest coin possible that doesn't overshoot the target, then repeat. For our setup, you take a 4, leaving you with 2 remaining. Since a 4 is now too big, you drop down to the next largest coin that fits, which is a 1, leaving 1. You grab one more 1, and you are done.
It feels logical, fast, and efficient. But step back and check if it is truly optimal. What if instead you picked two 3s?
Your greedy shortcut used 3 coins, but the actual minimum is just 2 coins. This failure mode proves that picking locally optimal pieces does not guarantee a globally minimum result, forcing us to explore a systematic way to check all overlapping subproblems.
Why Greedy Fails: Making Change with Coins [1, 3, 4] for Target = 6 Local optimum (Greedy) vs Global optimum (Dynamic Programming) Available Coins 1 3 4 Target Amount SUM = 6 Greedy Choice (Largest First) 4 Rem: 2 1 Rem: 1 1 Rem: 0 Result: 3 Coins 4 + 1 + 1 = 6 ❌ SUB-OPTIMAL Misses the shorter path Optimal Choice (Dynamic Prog.) 3 Rem: 3 3 Rem: 0 — Done Result: 2 Coins 3 + 3 = 6 ✔ TRUE MINIMUM Fewest coins possible
Why Greedy Fails: Making Change with Coins [1, 3, 4] diagram
Model

Defining DP State and Transitions for Minimum Coins

Phase 2: The Staircase of Subproblems

When greedy algorithms fail—like taking 4 first and getting stuck with (three coins) instead of the optimal (two coins)—we need a strategy that evaluates all paths without guessing. We transition from a local heuristic to a global search space by asking a simpler question: What is the minimum coin count to make every sub-amount from 0 up to our target 6?
We define an array dp where dp[x] represents the minimum number of coins needed to make amount . To fill this array, we build up from the base case of amount 0, which requires 0 coins (). For any other amount , if we decide to use a particular coin , the remaining amount we must make is .
This gives us our core recurrence relation:
For our working example where and , we can visualize each cell in our dp array as a step on a staircase. To reach step 6, we look backward by the size of each available coin (1, 3, and 4), inspect the minimum coins required at those previous steps, and add 1 (for the coin we just picked).
Coin Change DP Staircase: Amount 6 with Coins [1, 3, 4] Recurrence: dp[x] = min(dp[x], dp[x - coin] + 1) | Base Case: dp[0] = 0 Available Coins 1 3 4 Why Greedy Fails for 6 Take 4 first: 4 + 1 + 1 = 3 coins (Suboptimal) Optimal DP choice: 3 + 3 = 2 coins Amount 0 0 Base Case Amount 1 1 1[c=1] Amount 2 2 1+1 Amount 3 1 1[c=3] Amount 4 1 1[c=4] Amount 5 2 4+1 or 3+...? Amount 6 (Target) dp[6] = 2 Optimal: 3 + 3 -1 (dp[5]+1) -3 (dp[3]+1) -4 (dp[2]+1) How DP Evaluates All Choices for Amount 6: Try coin 1: Remaining amount is 5 → dp[5] + 1 = 2 + 1 = 3 coins Try coin 3: Remaining amount is 3 → dp[3] + 1 = 1 + 1 = 2 coins (Optimal!) Try coin 4: Remaining amount is 2 → dp[2] + 1 = 2 + 1 = 3 coins Core Recurrence Relation dp[x] = min(dp[x], dp[x - coin] + 1) By taking the minimum across all valid coin transitions, DP guarantees finding the absolute global optimum.
Defining DP State and Transitions for Minimum Coins diagram
Worked example

Tracing Coin Change for Amount 6 with Coins [1, 3, 4]

Phase 3: Working Through the DP Table

Let us trace our locked example: finding the minimum coins to make amount 6 using the coin set [1, 3, 4]. We will maintain an array dp of size 7 (indices 0 to 6), where dp[x] stores the minimum coins needed to make amount .

Given

- coins = [1, 3, 4]
- amount = 6
- Initial state: dp[0] = 0, and all other indices dp[1] through dp[6] initialized to (or a sentinel value like 7).

Steps

We iterate through each coin and update all reachable amounts from up to using the transition:
1. Processing coin 1:
- x = 1:
- x = 2:
- x = 3:
- x = 4:
- x = 5:
- x = 6:
- Table after coin 1: [0, 1, 2, 3, 4, 5, 6]
2. Processing coin 3:
- x = 3:
- x = 4:
- x = 5:
- x = 6:
- Table after coin 3: [0, 1, 2, 1, 2, 3, 2]
3. Processing coin 4:
- x = 4:
- x = 5:
- x = 6: (stays 2, since )
- Final table: [0, 1, 2, 1, 1, 2, 2]

Result

The final value at dp[6] is 2. This corresponds to using two 3-value coins (), successfully bypassing the suboptimal greedy choice of (3 coins).
Tracing DP Table for Amount 6 (Coins: [1, 3, 4]) Transition: dp[x] = min(dp[x], dp[x - coin] + 1) Available Coins: 1, 3, 4 idx: 0 0 idx: 1 1 idx: 2 2 idx: 3 1 idx: 4 1 idx: 5 2 idx: 6 2 Processing Coin 1 • Fills table linearly • dp[1..6] = [1, 2, 3, 4, 5, 6] Base incremental steps Establishes baseline path Processing Coin 3 • Updates jumps of 3 • dp[6] drops to min(6, 2) Bypasses pure +1 greedy Intermediate table: [.. 2] Processing Coin 4 & Result • Refines dp[4] & dp[5] • Final dp[6] = 2 Uses two 3-value coins (3+3) Beats greedy 4 + 1 + 1 (3 coins)
Tracing Coin Change for Amount 6 with Coins [1, 3, 4] diagram
Practice

Predicting DP Table States for a Custom Coin Denomination

Phase 4: Practice Your Transition Logic

Now that you have seen how the bottom-up dynamic programming table updates for amount 6 using coins = [1, 3, 4], it is time to test your mental model on a slightly different variation of the same system. Instead of target amount 6, let us shrink the target to 4 using the exact same coin denominations: coins = [1, 3, 4].
Before jumping to the full table, trace out what happens as you evaluate each coin for target amounts 1 through 4. Remember the recurrence relation from the previous phase:
Think about how dp[4] is formed directly by taking a single coin of value 4, compared to building it up from smaller sub-amounts like dp[3] + 1 or dp[1] + 3.
python
coins = [1, 3, 4]
amount = 4
# Predict the final value of dp[4] and the number of coins required.
# What intermediate values are stored in dp[0], dp[1], dp[2], dp[3], and dp[4]?
Apply

Applying the Min Coins DP Pattern to a New Target

Phase 5: Transfer

We have solved the locked example for amount 6 using coins , finding that the optimal solution requires 2 coins (), avoiding the greedy trap of . The fundamental recurrence relation and base case apply to any combination of coin denominations and target amounts.
To test this transfer of knowledge, consider how the table behaves if we alter the target amount or the coin set while keeping the exact same bottom-up execution structure. Recognizing when a problem maps directly to this unbounded knapsack-style recurrence allows you to bypass redundant search trees and solve optimization tasks efficiently in time.
python
def coinChangeTransfer(coins, amount):
    dp = [float('inf')] * (amount + 1)
    dp[0] = 0
    for x in range(1, amount + 1):
        for c in coins:
            if x - c >= 0:
                dp[x] = min(dp[x], dp[x - c] + 1)
    return dp[amount] if dp[amount] != float('inf') else -1

FAQ

Why does the greedy approach fail for the coin change problem with coins [1, 3, 4] and amount 6?
A greedy approach always picks the largest possible coin first. For amount 6 with [1, 3, 4], greedy picks 4 first, leaving amount 2, which requires two 1s (4 + 1 + 1 = 3 coins). However, the optimal solution is 3 + 3 = 2 coins. Dynamic programming evaluates all choices to avoid this trap.
What does the DP state dp[i] represent in the coin change problem?
dp[i] represents the minimum number of coins needed to make up the target amount i using the given coin denominations.
What is the time and space complexity of the coin change DP solution?
The time complexity is O(amount * n), where n is the number of coin denominations, because we compute the value for every amount from 1 to the target using each coin. The space complexity is O(amount) to store the DP table.
How does the algorithm handle amounts that cannot be formed, such as amount 3 with coins [2, 5]?
If an amount cannot be formed by any combination of the given coins, the DP state remains initialized to infinity (or amount + 1). After filling the table, if the target state is still unreachable, the function returns -1.

Keep learning