Coin Change Problem
Coin Change Problem
(/)
Ad closed by
Recurrences (/course/algorithms-recurrences/)
Heapsort (/course/algorithms-heapsort/)
QuickSort (/course/algorithms-quicksort/)
Backtracking (/course/algorithms-backtracking/)
https://www.codesdope.com/course/algorithms-coin-change/ 1/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
BFS (/course/algorithms-bfs/)
DFS (/course/algorithms-dfs/)
https://www.codesdope.com/course/algorithms-coin-change/ 2/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
Let's take a case of making 10¢ using these coins, we can do it in the following ways:
Out of these 4 ways of making 10¢, we can see that the first way of using only one coin of 10¢ requires the least number
of coins and thus it is our solution.
https://www.codesdope.com/course/algorithms-coin-change/ 3/15
10/26/2019 Coin Change Problem Using Dynamic Programming
Like the rod cutting problem (/course/algorithms-rod-cutting/), coin change problem also has the property of the optimal
substructure i.e., the optimal solution of a problem incorporates the optimal solution to the subproblems. For example, we
are making an optimal solution for an amount of 8 by using two values - 5 and 3. So, the optimal solution will be the
solution in which 5 and 3 are also optimally made, otherwise, we can reduce the total number of coins of optimizing the
values of 5 and 8.
The reason we are checking if the problem has optimal substructure or not because if there is optimal substructure, then
the chances are quite high that using dynamic programming will optimize the problem.
Let's say Mn is the minimum number of coins needed to make the change for the value n.
Let's start by picking up the first coin i.e., the coin with the value d1 . So, we now need to make the value of n − d1 and
Mn−d
1
is the minimum number of coins needed for this purpose. So, the total number of coins needed are 1 + Mn−d
1
(1
coin because we already picked the coin with value d1 and Mn−d
1
is the minimum number of coins needed to make the
rest of the value).
Similarly, we can pick the second coin first and then attempt to get the optimal solution for the value of n − d2 which will
require Mn−d
2
coins and thus a total of 1 + Mn−d
2
.
We can repeat the process with all the k coins and then the minimum value of all these will be our answer. i.e.,
mini:d
i ≤n
{Mn−d + 1}
i
.
The above process can also be understood in a different way. Suppose, we have picked a coin with value x and we know
that this coin is going to be in the solution. So, our next task is to find the minimum number of coins needed to make the
change of value n-x i.e., Mn−x . Also, by choosing the coin with value x, we have already increased the total number of
coins needed by 1. So, we can write:
Mn = 1 + Mn−x
https://www.codesdope.com/course/algorithms-coin-change/ 4/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
But the real problem is that we don't know the value of x. So, we will try every coin for the value of x and then we will
select the minimum among those.
Mn = {
0, if n = 0
You can see that there are overlapping subproblems in our solution and thus, we can use dynamic programming to
optimize this. So, let's look at the coding implementation of the formula we derived above.
We are going to use the bottom-up implementation of the dynamic programming to the code.
Our function is going to need the denomination vectors of coin (d), the value for which change has to be made (n) and
number of denominations we have (k or number of elements in the array d) i.e., COIN-CHANGE(d, n, k)
Let's start by making an array to store the minimum number of coins needed for each value i.e., M[n+1] .
M[0] = 0
Now, we need to iterate for each value in array the M and fill it up. Also to store the minimum number of coins for each
value, we will make a variable and initialize it with ∞ so that all other values are less than it in the starting. It is similar to
the variable 'maximum_revenue' we made in the previous chapter.
for j in 1 to n
minimum = INF
https://www.codesdope.com/course/algorithms-coin-change/ 5/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
Now for each j, we need to choose the minimum of Mn−d + 1
i
, where di will change from d1 to dk and thus, i will
change from 1 to k. Also, if the value of di is greater than j (value to be made), then of course, we can't use that coin.
for j in 1 to n
minimum = INF
for i in 1 to k
if j >= d[i]
minimum = min(minimum, 1 + M[j-d[i]])
if j >= d[i] → If the value to be made (j) is greater than the value of the current coin (d[i]), only then we can use this
coin.
Now we just need to change the value of the array M to the calculated minimum and return it.
for j in 1 to n
minimum = INF
for i in 1 to k
...
M[j] = minimum
return M[n]
COIN-CHANGE(d, n, k)
M[n+1]
M[0] = 0
for j in 1 to n
minimum = INF
for i in 1 to k
if j >= d[i]
minimum = min(minimum, 1+M[j-d[i]])
M[j] = minimum
return M[n]
C Python Java
https://www.codesdope.com/course/algorithms-coin-change/ 6/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
#include <stdio.h>
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
int i, j;
for(j=1; j<=n; j++) {
int minimum = INF;
int main() {
// array starting from 1, element at index 0 is fake
int d[] = {0, 1, 2, 3};
printf("%d\n", coin_change(d, 5, 3)); //to make 5. Number of denominations = 3
return 0;
}
Let's suppose we know the first coin needed to get the optimal solution of each value. Now, we can subtract the value of
the first coin from n and then we will have a different value to be made.
As discussed above, the problem exhibits optimal substructure and we know the first coin needed to make this new value,
so we know the second coin needed to make the value n .
So, the point is we only need to store the first coin needed for each value and then we can get the optimal coins for any
value.
Thus, we will modify the above code and store the first coins of each value in an array S.
https://www.codesdope.com/course/algorithms-coin-change/ 7/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
COIN-CHANGE-MODIFIED(d, n, k)
M[n+1]
M[0] = 0
S[n+1]
S[0] = 0
for j in 1 to n
minimum = INF
for i in 1 to k
if j >= d[i]
if 1+M[j-d[i]] < minimum
minimum = 1+M[j-d[i]]
coin = i
M[j] = minimum
S[j] = coin
We have just broken the 'min' function from the above code minimum = min(minimum, 1+M[j-d[i]]) to if 1+M[j-d[i]] <
minimum → minimum = 1+M[j-d[i]] . In this case, we are also assigning the value of i to the variable 'coin' because this coin
'i' is giving us the minimum value and thus, it will be the coin to be stored in the array S.
https://www.codesdope.com/course/algorithms-coin-change/ 8/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
COIN-CHANGE-MODIFIED(d, n, k)
M[n+1]
M[0] = 0
S[n+1]
S[0] = 0
for j in 1 to n
minimum = INF
for i in 1 to k
if j >= d[i]
if 1+M[j-d[i]] < minimum
minimum = 1+M[j-d[i]]
coin = i
M[j] = minimum
S[j] = coin
return M[n]
C Python Java
https://www.codesdope.com/course/algorithms-coin-change/ 9/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
#include <stdio.h>
int S[n+1];
S[0] = 0;
int i, j;
for(j=1; j<=n; j++) {
int minimum = INF;
int coin=0;
int l = n;
while(l>0) {
printf("%d\n",d[S[l]]);
l = l-d[S[l]];
}
return M[n];
}
int main() {
// array starting from 1, element at index 0 is fake
int d[] = {0, 1, 2, 3};
coin_change_modified(d, 5, 3);
return 0;
}
As said above, we are first printing the value of the coin (S[l] will give us the coin and d[S[l]] is the value of that coin).
After this, our initial value has decreased by the value of the coin i.e., l = l-d[S[l]] and we are doing this until we the
value is greater than 0 while l>0 .
The first loop is iterating from 1 to n and thus has a running time of Θ(n) . The last loop will also run a total of n times in
worst case (it can run faster depending upon the value of l-d[S[l]] ). Thus it has a running time of O(n) .
Now, there is also a nested loop. The first loop if iterating from 1 to n and the second is iterating from 1 to k and thus, a
total running time of Θ(nk) . All the other statements are constant time taking statements. Thus the algorithm has a
running time of Θ(nk) .
There are many other interesting algorithms which are optimized by dynamic programming. You can write about them on
BlogsDope (https://www.codesdope.com/blog/) or can download the BlogsDope app
(https://play.google.com/store/apps/details?id=com.blogsdope) to never miss any new articles.
https://www.codesdope.com/course/algorithms-coin-change/ 10/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
https://www.codesdope.com/course/algorithms-coin-change/ 11/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
(https://play.google.com/store/apps/details?
id=com.blogsdope&pcampaignid=MKT-
Other-global-all-co-prtnr-py-
PartBadge-Mar2515-1)
New Questions
(/discussion/for-loop-topic-
questions-and-answers)
Medlearnity - Other
(/discussion/medlearnity)
Welcome to
irononpatchesinc.com - Other
(/discussion/welcome-to-
irononpatchesinccom)
(/discussion/can-i-return-something-
inside-a-constractor)
(/discussion/write-a-program-to-find-
ith-largest-term-in-a-give)
(/discussion/compiling-in-mac-
mojave)
A s k Yo u r s
(/add_question/)
https://www.codesdope.com/course/algorithms-coin-change/ 12/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
(/discussion/)
(/media/pdf/ASCII.pdf)
(/practice/)
(https://www.facebook.com/codesdope/)
(https://twitter.com/CodesDope)
(https://www.linkedin.com/company/codesdope)
Select Language ▼
Recent Posts
(/blog/submit-article/)
FOLLOW US
(https://www.facebook.com/codesdope) (https://twitter.com/codesdope)
(https://www.pinterest.com/codesdope/) (https://www.linkedin.com/company/codesdope)
(https://plus.google.com/b/103889423774256807287/103889423774256807287/posts)
CATEGORIES
ABOUT (/about/)
BLOG (/blog/)
COURSES (/)
PRACTICE (/practice/)
DISCUSSION (/discussion/)
CONTACT US (/contact-us/)
https://www.codesdope.com/course/algorithms-coin-change/ 14/15
10/26/2019 Coin Change Problem Using Dynamic Programming
TERMS OF USE (/terms-of-use/) (/add_quest
PRIVACY POLICY (/privacy-policy/)
KEEP IN TOUCH
[email protected] (mailto:[email protected])
https://www.codesdope.com/course/algorithms-coin-change/ 15/15