Something: Coin change 1
42 COIN BUY | 442 |
Coin charger project | Jersey 5 coin |
Coin laundry bowie md | |
Gold coin online india |
Coin Change Problem
Given an unlimited supply of coins of given denominations, find the total number of distinct ways to get the desired change.
For example,
The total number of ways is 6
{ 1, 7 }
{ 3, 5 }
{ 1, 1, 3, 3 }
{ 1, 1, 1, 5 }
{ 1, 1, 1, 1, 1, 3 }
{ 1, 1, 1, 1, 1, 1, 1, 1 }
Input: S = { 1, 2, 3 }, N = 4
The total number of ways is 4
{ 1, 3 }
{ 2, 2 }
{ 1, 1, 2 }
{ 1, 1, 1, 1 }
The idea is to use recursion to solve this problem. We recur to see if the total can be reached by choosing the coin or not for each coin of given denominations. If choosing the current coin results in the solution, update the total number of ways.
Following is the C++, Java, and Python implementation of the idea:
C++
#include <iostream> // Function to find the total number of ways to get a change of `N` // from an unlimited supply of coins in set `S` intcount(intS[],intn,intN) // if the total is 0, return 1 if(N==0){ return1; } // return 0 if total becomes negative if(N<0){ return0; } // initialize the total number of ways to 0 intresult=0; // do for each coin for(inti=0;i<n;i++) { // recur to see if total can be reached by including // current coin `S[i]` result+=count(S,n,N-S[i]); } // return the total number of ways returnresult; // `n` coins of given denominations intS[]={1,2,3}; intn=sizeof(S)/sizeof(S[0]); // total change required intN=4; cout<<"The total number of ways to get the desired change is " <<count(S,n,N); return0; |
DownloadRun Code
Output:
The total number of ways to get the desired change is 7
Java
// Function to find the total number of ways to get a change // of `N` from an unlimited supply of coins in set `S` publicstaticintcount(int[]S,intN) { // if the total is 0, return 1 if(N==0){ return1; } // return 0 if total becomes negative if(N<0){ return0; } // initialize the total number of ways to 0 intresult=0; // do for each coin for(inti=0;i<S.length;i++) { // recur to see if total can be reached by including // current coin `S[i]` result+=count(S,N-S[i]); } // return the total number of ways returnresult; } publicstaticvoidmain(String[]args) { // `n` coins of given denominations int[]S={1,2,3}; // total change required intN=4; System.out.println("The total number of ways to get the desired change is " +count(S,N)); } |
DownloadRun Code
Output:
The total number of ways to get the desired change is 7
Python
# Function to find the total number of ways to get a change of `N` from an # unlimited supply of coins in set `S` # if the total is 0, return 1 ifN==0: return1 # return 0 if total becomes negative ifN<0: return0 # initialize the total number of ways to 0 result=0 # do for each coin foriinrange(len(S)): # recur to see if total can be reached by including # current coin `S[i]` result+=count(S,N-S[i]) # return the total number of ways returnresult if__name__=='__main__': # `n` coins of given denominations S=[1,2,3] # total change required N=4 print("The total number of ways to get the desired change is",count(S,N)) |
DownloadRun Code
Output:
The total number of ways to get the desired change is 7
The time complexity of the above solution is exponential since each recursive call is making recursive calls. It also requires additional space for the call stack.
There is an issue with the above solution. The above solution doesn’t always return distinct sets. For example, for set , it returns 7 as some ways are permutations of each other, as shown below:
{1, 1, 2}, {2, 1, 1}, {1, 2, 1}
{2, 2}
{1, 3}, {3, 1}
How can we get distinct ways?
The idea is somewhat similar to the Knapsack problem. We can recursively define the problem as:
That is, for each coin.
- Include current coin in solution and recur with remaining change with the same number of coins.
- Exclude current coin from solution and recur for remaining coins .
Finally, return the total ways by including or excluding the current coin. The recursion’s base case is when a solution is found (i.e., change becomes 0) or the solution doesn’t exist (when no coins are left, or total becomes negative). Following is the C++, Java, and Python implementation of the idea:
C++
#include <iostream> // Function to find the total number of distinct ways to get a change of `N` // from an unlimited supply of coins in set `S` intcount(intS[],intn,intN) // if the total is 0, return 1 (solution found) if(N==0){ return1; } // return 0 (solution does not exist) if total becomes negative, // no elements are left if(N<0||n<0){ return0; } // Case 1. Include current coin `S[n]` in solution and recur // with remaining change `N-S[n]` with the same number of coins intinclude=count(S,n,N-S[n]); // Case 2. Exclude current coin `S[n]` from solution and recur // for remaining coins `n-1` intexclude=count(S,n-1,N); // return total ways by including or excluding current coin returninclude+exclude; // `n` coins of given denominations intS[]={1,2,3}; intn=sizeof(S)/sizeof(S[0]); // total change required intN=4; cout<<"The total number of ways to get the desired change is " <<count(S,n-1,N); return0; |
DownloadRun Code
Output:
The total number of ways to get the desired change is 4
Java
// Function to find the total number of distinct ways to get // a change of `N` from an unlimited supply of coins in set `S` publicstaticintcount(int[]S,intn,intN) { // if the total is 0, return 1 (solution found) if(N==0){ return1; } // return 0 (solution does not exist) if total becomes negative, // no elements are left if(N<0||n<0){ return0; } // Case 1. Include current coin `S[n]` in solution and recur // with remaining change `N-S[n]` with the same number of coins intincl=count(S,n,N-S[n]); // Case 2. Exclude current coin `S[n]` from solution and recur // for remaining coins `n-1` intexcl=count(S,n-1,N); // return total ways by including or excluding current coin returnincl+excl; } // Coin Change Problem publicstaticvoidmain(String[]args) { // `n` coins of given denominations int[]S={1,2,3}; // total change required intN=4; System.out.print("The total number of ways to get the desired change is " +count(S,S.length-1,N)); } |
DownloadRun Code
Output:
The total number of ways to get the desired change is 4
Python
# Function to find the total number of distinct ways to get a change of `N` # from an unlimited supply of coins in set `S` # if the total is 0, return 1 (solution found) ifN==0: return1 # return 0 (solution does not exist) if total becomes negative, # no elements are left ifN<0orn<0: return0 # Case 1. Include current coin `S[n]` in solution and recur # with remaining change `N-S[n]` with the same number of coins incl=count(S,n,N-S[n]) # Case 2. Exclude current coin `S[n]` from solution and recur # for remaining coins `n-1` excl=count(S,n-1,N) # return total ways by including or excluding current coin returnincl+excl if__name__=='__main__': # `n` coins of given denominations S=[1,2,3] # total change required N=4 print("The total number of ways to get the desired change is", count(S,len(S)-1,N)) |
DownloadRun Code
Output:
The total number of ways to get the desired change is 4
The time complexity of the above solution is still exponential and requires auxiliary space for the call stack.
The problem has an optimal substructure as the problem can be broken down into smaller subproblems, which can further be broken down into yet smaller subproblems, and so on. The problem also clearly exhibits overlapping subproblems
-
-