Coin Change Problem Recursive Solution
by hemangsarkar, Dec 31, 2015, 6:32 PM
Q) You are given a set A which has the values of say m coins and a positive integer n. You are asked to find the number of different ways you can use these coins to make a total of n. Assume you have an infinite supply of each type of coin.
Example: A = {1,2,3} and n=4. You can have 4 = 1 + 1 + 1 + 1, 1 + 3, 2+2, 2+1+1. Note that the order of the coins doesn't matter. There are 4 different ways to get 4 using these coins.
Solution:
Though the recursive solution is not at all optimal, it is good for a start to understand the problem.
Take any one coin from the set A.
Using this coin, we can divide the solution space into two parts. The first part uses this coin to get to n and the second part doesn't. These two parts are mutually exclusive and so if we add the number of solutions in each, we get the final answer.
We now make a function which illustrates the idea.
Here int A[] is the given array of coin values.
It is a 0 indexed array, so the values are A[0], A[1], ... , A[m-1]
int count( int A[], int m, int n )
{
// If n is 0 then there is 1 solution: do not include any coin
if (n == 0)
return 1;
// If n is less than 0 then no solution exists
if (n < 0)
return 0;
// If there are no coins and n is greater than 0, then no solution exists
if (m <=0 && n > 0)
return 0;
return count( A, m - 1, n ) + count( A, m, n-A[m-1] );
/*count( A, m - 1, n ) means the number of solutions which don't include A[m-1] and thus only have elements from A[0] to A[m-2].
and count(A, m, n-A[m-1]) has already included A[m-1] once. It might well come again and so we don't change m to m-1*/
}
Example: A = {1,2,3} and n=4. You can have 4 = 1 + 1 + 1 + 1, 1 + 3, 2+2, 2+1+1. Note that the order of the coins doesn't matter. There are 4 different ways to get 4 using these coins.
Solution:
Though the recursive solution is not at all optimal, it is good for a start to understand the problem.
Take any one coin from the set A.
Using this coin, we can divide the solution space into two parts. The first part uses this coin to get to n and the second part doesn't. These two parts are mutually exclusive and so if we add the number of solutions in each, we get the final answer.
We now make a function which illustrates the idea.
Here int A[] is the given array of coin values.
It is a 0 indexed array, so the values are A[0], A[1], ... , A[m-1]
int count( int A[], int m, int n )
{
// If n is 0 then there is 1 solution: do not include any coin
if (n == 0)
return 1;
// If n is less than 0 then no solution exists
if (n < 0)
return 0;
// If there are no coins and n is greater than 0, then no solution exists
if (m <=0 && n > 0)
return 0;
return count( A, m - 1, n ) + count( A, m, n-A[m-1] );
/*count( A, m - 1, n ) means the number of solutions which don't include A[m-1] and thus only have elements from A[0] to A[m-2].
and count(A, m, n-A[m-1]) has already included A[m-1] once. It might well come again and so we don't change m to m-1*/
}