Introduction
The Coin Change Problem is a classic and very important Dynamic Programming (DP) problem in DSA. It is frequently asked in coding interviews because it tests your understanding of optimization, decision making, and DP table construction.
In simple words, this problem is about finding the minimum number of coins needed to make a given amount using the available coin denominations.
This article explains the Coin Change problem in very simple, human-friendly language, starting from the basics and progressing to interview-level understanding.
What is the Coin Change Problem?
You are given:
An array of coin denominations
A target amount
Your task is to find the minimum number of coins required to make that amount.
Important rules:
You can use each coin any number of times
If it is not possible to make the amount, return -1
Example
Coins = [1, 2, 5]
Amount = 11
Output
3
Explanation
The minimum number of coins needed to make 11 is:
5 + 5 + 1 = 11
So the answer is 3 coins.
Why Greedy Approach Does Not Always Work
A greedy approach always picks the largest coin first.
This may work for some cases but fails for others.
Example Where Greedy Fails
Coins = [1, 3, 4]
Amount = 6
Greedy picks:
4 + 1 + 1 = 3 coins
But optimal solution is:
3 + 3 = 2 coins
So we need a better approach.
Why Dynamic Programming is Needed
Dynamic Programming helps us:
Break the problem into smaller subproblems
Store results to avoid recomputation
Find the optimal solution efficiently
This makes DP the best choice for the Coin Change problem.
Dynamic Programming Approach (Core Idea)
DP State Definition
We create a DP array where:
dp[i]= minimum number of coins needed to make amounti
Base Case
dp[0] = 0(0 coins are needed to make amount 0)
All other values are initialized to a large number.
Step-by-Step DP Explanation
Steps:
Create a DP array of size
amount + 1Initialize all values with a large number
Set
dp[0] = 0For each amount from 1 to target amount:
Try every coin
If coin value ≤ current amount:
Update
dp[current] = min(dp[current], dp[current - coin] + 1)
If
dp[amount]is still large, return -1Otherwise, return
dp[amount]
Dry Run Example
Coins = [1, 2, 5], Amount = 11
Join the conversation! Your thoughts help the community grow.