Minimum Coin Change | Find minimum number of coins that make a given value
Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
Input:
The test case consists of V and N, V is the value of cents and N is the number of coins. The second line of each test case contains N input C[i], the value of available coins.
Output:
Print the minimum number of coins to make the change, if not possible print "-1".
Example with explanation:
Input:
N=5, V = 25
coins[] = {4,5,2,1,9}.
Output:
Minimum 4 coins required
We can use one coin of 5 cents,
two coins of 9 cents and one of 2 cents
(9+9+2+5)
Input:
N=6, V = 256324
coins[] = {1,2,5,10,20,50}.
Output:
Minimum 5129 coins required
1) Recursive solution
We will start the solution with initially sum = V cents and at each iteration find the minimum coins required by dividing the problem into subproblems where we take {C1, C2, ..., CN} coin and decrease the sum V.
By C[i] (based on the coin we took). Whenever V becomes 0, this means we have a possible or otherwise there is no solution and we return -1;
Solution: To find the optimal answer, we return the minimum of all answers for which N became 0.
If V == 0, then 0 coins required.
We calculate the total required number of coins using the following function:
So, initially the function call would be:
Where C is the coins array, N is the size of the coin array and V is the required sum.
Now our idea is the get the minimum number of coins from the given array
and if the sum that we want to get if not possible from the given set of coins we will return -1.
So, let's see the function definition:
Function:
Pseudo Code:
The recursive definition consists of the part:
This recursive part will generate a recursion tree where we can find many overlapping subproblems, hence we would solve by dynamic programming.
C++ Implementation:
Output
2) Dynamic Programming Approach
Since the same subproblems are called, again and again, this problem has Overlapping Subproblems property.
Like other typical Dynamic Programming (DP) problems, re-computations of the same subproblems can be avoided by constructing a temporary array dp[] and memorizing the computed values in this array.
We will declare a dp[] array of length equal to the sum required+1 since indexing starts from 0, we will use Top-Down DP, use memoization plus recursion approach.
Initially, we will fill the dp[] array with -1 so that during recursion call if the array if not -1 then we don't have to calculate the value we just need to return the dp[value](memoized array), else we will call the function and fill the required value of the array.
2.1) Top Down Approach
pseudo code:
C++ Implementation:
Output
2.2) Bottom Up Approach
ith state of dp : dp[i] : Minimum number of coins required to sum to i cents.
In this approach, we move from the base case to the desired value of the sum
base dp[0]=0 , where for 0 cents of value we need 0 coins.
Initially, we will fill the dp[] array with INT_MAX and dp[0]=0 as for 0 cents we need 0 coins.
Then we will iterate from 1 cent required sum to V value cents.
we will use the outer loop for sum array and inner loop for coins array.
Pseudo Code:
The overall Time complexity of this approach is O(N*V).
C++ Implementation:
Output