Difficulty: Medium, Asked-in: Microsoft, Amazon, Morgan Stanley.
Key takeaway: An excellent problem to learn problem-solving using dynamic programming.
Let’s understand the problem
We are given amount K and integer array coin[] of size m representing coins of different denominations. Write a program to find the minimum number of coins required to make change. We assume that we have an infinite supply of each coin. If any combination of the coins cannot make up amount K, return -1.
Example 1
Input: coin[] = [25, 10, 5], K = 30, Output: 2.
Explanation: Minimum 2 coins required: we can use one coin of 25 and one coin of 5.
Example 2
Input: coin[] = [9, 6, 5, 1], K = 13, Output: 3.
Explanation: Minimum 3 coins required: we can use two coins of 6 and one coin of 1.
Example 3
Input: coin[] = [1, 3, 5, 7], K = 18, Output: 4.
Explanation: Minimum 4 coins required. We can use any one of these combinations to provide change using 4 coins: (7, 7, 3, 1),(5, 5, 5, 3), and (7, 5, 5, 1).
Discussed solution approaches
- Brute force approach using recursion
- Efficient approach using bottom-up approach of dynamic programming
Brute force approach using recursion
Solution idea and steps
This is an optimisation problem because there can be several ways to provide change, but we need to return the change using the minimum number of coins. So one basic idea would be to explore all possibilities of the change and return the count with a minimum number of coins. How do we implement this? Let’s think!
- When we need to explore all possibilities, we can think about solving the problem recursively, i.e., the solution of the problem using smaller sub-problems.
- Initially, available choices are important for building the recursive solution. Here we have m choices of coins at the start, i.e., we can pick any coin among m coins.
- Suppose minCoinChange(coin[], m, K) returns the minimum number of coins required to make the change of value K. If we select any coin[i] first, the smaller sub-problem is minCoinChange(coin[], m, K - coin[i]), i.e., the minimum number of coins required to make a change of amount K - coin[i].
- So, for i = 0 to m - 1, whichever choice provides the change using a minimum number of coins, we shall add 1 and return the value. Before selecting any coin, make sure whether the value of the coin is less than or equal to the amount, i.e., coin[i] <= K.
Recursive structure:
minCoinChange(coin[], m, K) = min(for i = 0 to m - 1){1 + minCoinChange(coin[], m, K - coin[i])}
Where coin[i] <= K.
Base case: If K == 0, then 0 coins are required.
Solution pseudocode
minCoinChange(coin, m, K)
if K == 0
return 0
minCount = INT_MAX
// Try each coin that does not exceed the remaining amount
for i from 0 to m - 1
if coin[i] <= K
currCount = minCoinChange(coin, m, K - coin[i])
// Update the minimum if the remaining amount is achievable
if currCount != -1 and currCount + 1 < minCount
minCount = currCount + 1
// Return -1 if no combination can make amount K
if minCount == INT_MAX
return -1
return minCountTime and space complexity analysis
The total number of recursive calls increases as the values of K or m increase, so the time complexity depends on both K and m. To estimate the upper bound, assume that coin[i] <= K at every recursive call. In this case, the recursion tree is a full tree in which each node has m children. It has m recursive calls at the first level, m² at the second level, and so on.
The total number of recursive calls in the worst-case = 1 + m + m^2 + ... + m^k = (m^(k+1) - 1) / (m - 1) = O(m^k), which is exponential. Note that the sum of the geometric progression S = a + ar + ar^2 + ... + ar^n = a(r^(n+1) - 1)/(r - 1), where r > 1.
In retrospect, this is not surprising because we are exploring all possibilities to provide change. If we create a recursion tree for the above approach, we can clearly notice repeated subproblems. For example, the following picture shows the recursion tree diagram for K = 5 and coins of given values [1, 2, 3]. The sub-problem of size 3 appears 2 times, the sub-problem of size 2 appears 4 times, and the sub-problem of size 1 appears 7 times.

Efficient approach using bottom-up approach
Solution idea and steps
Since we have identified that it is a dynamic programming problem, we can solve it using the bottom-up approach. Our aim here is to calculate the solution to smaller problems in an iterative way and store their results in a table. The critical question is: How do we build the solution in a bottom-up manner? Let’s think!
- Table structure: The state of the sub-problems depends on the variable K because it decreases after each recursive call. So we need to construct a 1-D table to store the solutions of the sub-problems.
- Table size: The size of the 1-D table is equal to the number of different subproblems. According to the recursion tree, there can be a total of (K + 1) different sub-problems, i.e., sub-problems of size K, K-1, ..., 2, 1, 0. So we use the table of size K + 1 i.e. Change[K + 1].
- Table initialisation: Before building the solution using bottom-up approach, we need to initialize the table with the base case, which is Change[0] = 0.
- Iterative structure to fill the table: Now, we define an iterative structure to fill the table Change[i], i.e., a relation by which we build the solution of larger problems using the solutions of smaller problems in a bottom-up manner. We can easily define iterative structure by using the recursive structure of the recursive solution. Change[i] = min (for j = 0 to m-1) {1 + Change[i - coin[j]]}, where coin[j] <= i. Here, Change[i] represents the minimum number of coins needed to make amount i.
- Returning the final solution: After filling the table iteratively, our final solution is stored at the last index of the array, i.e., we return Change[K].
Solution pseudocode
miniCoinChange(coin, m, K)
if K == 0
return 0
Change = array of size K + 1
Change[0] = 0
// Initialize other amounts as unreachable
for i from 1 to K
Change[i] = INT_MAX
// Find the minimum number of coins needed for each amount
for i from 1 to K
for j from 0 to m - 1
if coin[j] <= i
currCount = Change[i - coin[j]]
// Update the count if the remaining amount is reachable
if currCount != INT_MAX and currCount + 1 < Change[i]
Change[i] = currCount + 1
// Return -1 if amount K cannot be formed
if Change[K] == INT_MAX
return -1
else
return Change[K]Time and space complexity analysis
There are two nested loops. The first loop iterates from 1 to K, and the second loop iterates from 0 to m-1. The time complexity is O(K*m), and the space complexity is O(K) for the extra array Change[].
Critical ideas to think!
- Do we require the entire array of size K, or can we optimize the space? Think about the case when the amount is too high.
- How can we modify the code to find the coins that are part of the optimal solution?
- Can we solve this problem using some other approach? Can we think of applying the greedy or divide-and-conquer strategy?
- Explore DP problems that can be solved using a similar idea.
- Consider the top-down approach to solving this problem.
Suggested problems to solve
- Find the number of ways in which you can change an amount with given coins of different denominations.
- Greedy algorithm to find the minimum coin count.
- Rod cutting problem
- 0–1 Knapsack problem
- Weighted job scheduling
If you have any queries/doubts/feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy algorithms!

