Coin Change Problem using Dynamic Programming

This is a C++ Program that Solves Coin Change Problem using Dynamic Programming technique.

Problem Description

There are infinite number of coins of x different values. These values are given. Using these coins, you have to make change for Rs. N. In how many ways, you can make this change?

Note that there are infinite number of coins of each given value.

Problem Solution

This problem can be solved by using Bottom-up dynamic programming. First we will calculate the no. of ways to change a smaller amount. This can be calculated by finding out no. of ways to change the required amount by once including a coin and once excluding it. Then we will store this value in a matrix. Now, using these values, we will calculate the result for a bigger amount.

The time complexity of this solution is O(N*x).

Expected Input and Output

Case-1:

advertisement
advertisement
N=10
x=4
coin[]={2,5,3,6}
 
Expected result=5 
 
Possible combinations are -
2+2+2+2+2
5+5
2+3+5
2+2+3+3
2+2+6

Case-2:

N=4
x=3
coin[]={1,2,3}
 
Expected result=4
 
Possible combinations are -
1+1+1+1
1+1+2
1+3
2+2
Program/Source Code

Here is source code of the C++ Program to Solve Coin Change Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int coinChange(int coin[], int x, int N)
  5. {
  6.     int i,j;
  7.  
  8.     //make a matrix of size (N+1)*x to tabulate the computed values 
  9.     int dp[N+1][x];
  10.  
  11.     //dp[i][j] represents no.of ways in which change of Rs. i can be made with just j type of coins available
  12.  
  13.     int including,excluding;
  14.  
  15.     //initialization
  16.     //since, there is only one way in which change of Rs. 0 can be made ie., not including any coin
  17.     for(j=0;j<x;j++)
  18.     {
  19.         dp[0][j]=1;
  20.     }
  21.  
  22.     //i represents the amount whose change is required
  23.     for(i=1;i<=N;i++)
  24.     {
  25.         //j represents the coin values available
  26.         for(j=0;j<x;j++)
  27.         {
  28.             //if the the value of current coin is less than or equal to total amount whose change is required
  29.             //include this coin
  30.             if(i>=coin[j])
  31.             {
  32.                 including=dp[i-coin[j]][j];
  33.             }
  34.  
  35.             else
  36.                 including=0;
  37.  
  38.             //excluding will store the number of ways in which the amount can be changed without including the current coin value
  39.             if(j>=1)
  40.                 excluding=dp[i][j-1];
  41.  
  42.             else
  43.                 excluding=0;
  44.  
  45.             //dp[i][j] will be the sum of no.of ways by including as well as excluding the current coin
  46.             dp[i][j]=including+excluding;
  47.         }
  48.     }
  49.  
  50.     return dp[N][x-1];
  51. }
  52.  
  53. int main()
  54. {
  55.     int i;
  56.     int x;  //number of distinct values of coins
  57.     int N;  //amount whose change is required
  58.  
  59.     cout<<"Enter the amount whose change is required"<<endl;
  60.     cin>>N;
  61.  
  62.     cout<<"Enter the number of distinct values of coins"<<endl;
  63.     cin>>x;
  64.  
  65.     //distinct values of coins
  66.     int coin[x];
  67.  
  68.     cout<<"Enter the values of coins"<<endl;
  69.     for(i=0;i<x;i++)
  70.         cin>>coin[i];
  71.  
  72.     cout<<"No. of ways in which Rs."<<N<<" can be changed is ";
  73.     cout<<coinChange(coin,x,N);
  74.  
  75.     cout<<endl;
  76.     return 0;
  77. }
Program Explanation

In the main function, we ask the user to input the amount of change required, number of coins available and the value of coins. We pass these values to the function coinChange as parameters. This function will calculate the expected result and return it. The returned value will be displayed.

advertisement
Runtime Test Cases
 
Case-1:
$ g++ coin_change.cpp
$ ./a.out
Enter the amount whose change is required
10
Enter the number of distinct values of coins
4
Enter the values of coins
2
5
3
6
No. of ways in which Rs.10 can be changed is 5

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.