Dynamic Programming – Coin Change Problem

Objective: Given a set of coins and amount, Write an algorithm to find out how many ways we can make the change of the amount using the coins given.

This is another problem in which i will show you the advantage of Dynamic programming over recursion.


Amount = 5

coins [] = {1,2,3}

Ways to make change = 5

{1,1,1,1,1} {1,1,1,2}, {1,2,2}, {1,1,3} {2,3}


Recursive Solution:

  • We can solve it using recursion.
  • For every coin we have an option to include it in solution or exclude it.
  • See the Code


Time Complexity : 2n

I have been asked that by many readers that how the complexity is 2^n . So including a simple explanation-

For every coin we have 2 options, either we include it or exclude it so if we think in terms of binary, its 0(exclude) or 1(include). so for example if we have 2 coins, options will be 00, 01, 10, 11. so its 2^2. for n coins , it will be 2^n. In all these options we will be checking whether that selection has made the change which is required.

But if we notice that we are solving many sub-problems repeatedly.

We can do better by applying Dynamic programming.

Dynamic Programming: Bottom-up

Earlier we have seen “Minimum Coin Change Problem“. This problem is slightly different than that but approach will be bit similar.

Create a solution matrix. (solution[coins+1][amount+1]).

Base Cases:

  • if amount=0 then just return empty set to make the change, so 1 way to make the change.
  • if no coins given, 0 ways to change the amount.

Rest of the cases:

  • For every coin we have an option to include it in solution or exclude it.
  • check if the coin value is less than or equal to the amount needed, if yes then we will find ways by including that coin and excluding that coin.
  1. Include the coin: reduce the amount by coin value and use the sub problem solution (amount-v[i]).
  2. Exclude the coin: solution for the same amount without considering that coin.
  • If coin value is greater than the amount then we can’t consider that coin, so solution will be without considering that coin.



= 0 if i=0
solution[i][j] = 1 if j=0
= solution[i – 1][j] + solution[i][j – v[i – 1]] if(coin[i]<=j)
= solution[i – 1][j]; if(coin[i]>j)


Amount = 5
coins [] = {1,2,3}

Coin Change Problem


By Dynamic Programming 5

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

8 Responses

  1. Ankit says:

    Here is a code which prints the optimal coin change required to make a sum of money.


  2. .NetGeek says:

    C# Implementation for both recursive n DP: http://ideone.com/aaTymC

  3. Konstantin Zherdev says:

    Hi, if I understand correctly, in recursive implementation the third condition should be (i 0), not (i = v.length & n >0). The reason is that we check the border condition where there are no coins but still positive nominal. Probably also in return statement there should be i -1, not i+1.

  4. Ajay Pilaniya says:

    It can be done using 1D DP also

  5. Asen says:

    This coded mises few test case
    amount = 250
    41 34 46 9 37 32 42 21 7 13 1 24 3 43 2 23 8 45 19 30 29 18 35 11

    • sandeep tiwari says:

      please use “int[][] solution = new int[v.length + 1][amount + 1];” array as long “long[][] solution = new long[v.length + 1][amount + 1];” instaed of int.

  1. August 9, 2015

    […] got stuck and am trying to understand a Solution to this problem. Here is the recursive solution to this problem.(with my comments to understand […]

Leave a Reply to .NetGeek Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: