Ways To Make Coin Change
Posted: 27 Jul, 2020
Difficulty: Moderate
You are given an infinite supply of coins of each of denominations D = {D0, D1, D2, D3, ...... Dn-1}. You need to figure out the total number of ways W, in which you can make a change for value V using coins of denominations from D. Print 0, if a change isn't possible.
Input Format
The first line of input contains an integer N, representing the total number of denominations.
The second line of input contains N integers values separated by a single space. Each integer value represents the denomination value.
The third line of input contains the value of V, representing the value for which the change needs to be generated.
Output Format:
For each test case, print an integer denoting the total number of ways W, in which a change for V is possible.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= N <= 10
1 <= D[i] <=10^5
1 <= V <= 2 * 10^3
Where 'D[i]' represent the value of ith denomination.
Time Limit: 1sec
Approach 1
- The idea is to use recursion.
- For a particular coin, we have two options either include it or exclude it.
- If we include that coin, then calculate the remaining number that we have to generate so recur for that remaining number.
- If we exclude that coin, then recur for the same amount that we have to make.
- Our final answer would be the total number of ways either by including or excluding.
- There will be two edge cases in recursion that are: if the total amount we have is zero then return 1 (solution found) and if the total amount we have become negative or no elements are left then return zero that is if (n < 0 || value < 0) then return 0.
Approach 2
- The idea is to use recursion and memoization. So first create a 2D array 'memo' that will store the answer of all subproblems that we compute.
- For a particular coin, if the answer is already present in the map, then just return it.
- Otherwise, we have two options either include it or exclude it.
- If we include that coin, then calculate the remaining number that we have to generate so recur for that remaining number.
- If we exclude that coin, then recur for the same amount that we have to make.
- Our final answer would be the total number of ways either by including or excluding.
- Store the answer on the map before returning.
Approach 3
- The idea is to use dynamic programming.
- Create a two-dimensional array, ‘ways’, where ‘ways[i][j]’ will denote the total number of ways to make j value by using i coins.
- Fill dp array by the recurrence relation as follows:
ways[i][j] = ways[i-1][j] + ways[i][j-denominations[i]]
Where first term represents that we have excluded that coin and,
Second term represents that we have included that coin.
Approach 4
SIMILAR PROBLEMS
Prime Digit Sum
Posted: 17 Apr, 2022
Difficulty: Hard
Count Numbers Containing Given Digit K Times
Posted: 20 Apr, 2022
Difficulty: Moderate
Count Numbers With Equal First and Last Digit
Posted: 20 Apr, 2022
Difficulty: Moderate
Max Non Adjacent sum
Posted: 10 May, 2022
Difficulty: Easy
Mario And His Princess
Posted: 12 May, 2022
Difficulty: Moderate