# Understanding The Coin Change Problem With Dynamic Programming

The **Coin Change Problem** is considered by many to be essential to understanding the paradigm of programming known as **Dynamic Programming**. The two often are always paired together because the coin change problem encompass the concepts of dynamic programming. For those who don’t know about dynamic programming it is according to Wikipedia,

“both a mathematical optimization method and a computer programming method … it refers to simplifying a complicated problem by breaking it down into simpler sub-problems”.

In other words, dynamic problem is a method of programming that is used to simplify a problem into smaller pieces. For example if you were asked simply what is 3 * 89? you perhaps would not know the answer off of your head as you probably know what is 2 * 2. However, if you knew what was 3 * 88 (264) then certainly you can deduce 3 * 89. All you would have to do is add 3 to the previous multiple and you would arrive at the answer of 267. Thus, that is a very simple explanation of what is dynamic programming and perhaps you can now see how it can be used to solve large time complexity problems effectively.

By keeping the above definition of dynamic programming in mind, we can now move forward to the **Coin Change Problem**. The following is an example of one of the many variations of the coin change problem. Given a list of coins i.e **1 cents, 5 cents and 10 cents**, can you determine the total number of permutations of the coins in the given list to make up the number **N**?

**Example 1**: Suppose you are given the coins 1 cent, 5 cents, and 10 cents with N = 8 cents, what are the total number of permutations of the coins you can arrange to obtain 8 cents.

Input : N=8 Coins : 1, 5, 10 Output : 2 Explanation: 1 way: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8 cents. 2 way: 1 + 1 + 1 + 5 = 8 cents.

All you’re doing is determining all of the ways you can come up with the denomination of 8 cents. Eight 1 cents added together is equal to 8 cents. Three 1 cent plus One 5 cents added is 8 cents. So there are a total of 2 ways given the list of coins 1, 5 and 10 to obtain 8 cents.

**Example 2**: Suppose you are given the coins 1 cent, 5 cents, and 10 cents with N = 10 cents, what are the total number of permutations of the coins you can arrange to obtain 10 cents.

Input : N=10 Coins : 1, 5, 10 Output : 4 Explanation: 1 way: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 10 cents. 2 way: 1 + 1 + 1 + 1 + 1 + 5 = 10 cents. 3 way: 5 + 5 = 10 cents. 4 way: 10 cents = 10 cents.

Now that we know the problem statement and how to find the solution for smaller values, how would we determine the total number of permutations of coins that add to **larger values**? We write a program. How do we write the program to compute all of the ways to obtain larger values of N? simple we use **dynamic programming**. Remember the idea behind dynamic programming is to cut each part of the problem into smaller pieces. Similar to the example at the top of the page. If we don’t know the value of 4 * 36 but know the value of 4 * 35 (140), we can just add 4 to that value and get our answer for 4 * 36 which by the way is 144.

Okay so we understand what we have to do, but how is a program going to determine how many ways the list of coins can output N? Well lets look that this example.

N = 12Index of Array:[0, 1, 2]Array of coins:[1, 5, 10]

This is a array of coins, 1 cent, 5 cents, and 10 cents. The N is 12 cents. So we need to come up with a method that can use those coin values and determine the number of ways we can make 12 cents.

Thinking dynamically, we need to figure out how to add to previous data. So what that means is we have to add to previous solutions instead of recalculating over the same values. Clearly, we have to iterate through the entire array of coins. We also need a way to see if a coin is larger than the N value.

One way to do this is having an array that counts all the way up to the **Nth value**.

So …

**Array of ways:**

[0, 0, 0 ..... Nth value] in our case it would be up to 12.

The reason for having an array up to the Nth value is so we can determine the number of ways the coins make up the values at the index of **Array of ways**. We do this because if we can determine a coin is larger than that value at the index then clearly we can’t use that coin to determine the permutations of the coins because that coin is larger than that value. This can be better understood with an example.

Using the above numbers as example.

N = 12Index of Array of Coins:[0, 1, 2]Array of coins:[1, 5, 10]Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Before we start iterating we have to give a predefined value to our ways array. We must set the first element at index 0 of the ways array to 1. This is because there is 1 way to make the number 0, using 0 coins.

So if we started iterating through all the coins array and compare the elements to the Array of ways we will determine how many times a coin can be used to make the values at the index of the ways array.

For example…

First set ways[0] = 1.

Lets compare the first coin, 1 cent.

N = 12Index of Array of Coins:[0, 1, 2]Array of coins:[1, 5, 10]Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Then compare coins[0] to all of the index's of ways array. If the value of the coin is less than or equal to the ways index, then ways[j-coins[i]]+ways[j] is the new value of ways[j]. We do this because we are trying to break each part down into smaller pieces. You will see what is happening as you continue to read. So comparing each value of the ways index to the first coin, we get the following.Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Lets now compare the second coin, 5 cents.

N = 12Index of Array of Coins:[0, 1, 2]Array of coins:[1, 5, 10]Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] Comparing 5 cents to each of the index and making that same comparison, if the value of the coin is smaller than the value of the index at the ways array then ways[j-coins[i]]+ways[j] is the new value of ways[j]. Thus we get the following.Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3] We are determining how many times the second coin goes into all of the values leading up theNthcoin. Why are we using all of the coins? It is to check our previous result dynamically and update our answer instead of recalculating all over again. For example take the element at index 10 the answer is 3 so far. But how did we get 3? We know that the value of 10-5 is 5 so that is our j-coins[i] value, that is the difference of what needs to be made up to make the amount 10. So we look at index 5 of the ways array and see it has the value 2, for the same reason as above, there are so far 2 ways to obtain the value 5. So if there are 2 ways to obtain the value 5 then those ways plus the current number of ways is the new updated value of theTOTALways to get the value at index 10.

Lets now compare the third coin, 10 cents.

N = 12Index of Array of Coins:[0, 1, 2]Array of coins:[1, 5, 10] Comparing 10 cents to each of the index and making that same comparison, if the value of the coin is smaller than the value of the index at the ways array then ways[j-coins[i]]+ways[j] is the new value of ways[j]. Thus we get the following.Index of Array of ways:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]Array of ways:[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 4, 4] So the answer to our example is ways[12] which is 4.

With all of the above in mind, lets have a look at the following **JAVA** program below.

## Java

`/* We have input values of N and an array Coins ` ` ` `that holds all of the coins. We use data type ` ` ` `of long because we want to be able to test ` ` ` `large values ` `without integer overflow*/` ` ` `class` `getWays { ` ` ` ` ` `static` `long` `getNumberOfWays(` `long` `N, ` `long` `[] Coins) ` ` ` `{ ` ` ` `// Create the ways array to 1 plus the amount ` ` ` `// to stop overflow ` ` ` `long` `[] ways = ` `new` `long` `[(` `int` `)N + ` `1` `]; ` ` ` ` ` `// Set the first way to 1 because its 0 and ` ` ` `// there is 1 way to make 0 with 0 coins ` ` ` `ways[` `0` `] = ` `1` `; ` ` ` ` ` `// Go through all of the coins ` ` ` `for` `(` `int` `i = ` `0` `; i < Coins.length; i++) { ` ` ` ` ` `// Make a comparison to each index value ` ` ` `// of ways with the coin value. ` ` ` `for` `(` `int` `j = ` `0` `; j < ways.length; j++) { ` ` ` `if` `(Coins[i] <= j) { ` ` ` ` ` `// Update the ways array ` ` ` `ways[j] += ways[(` `int` `)(j - Coins[i])]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// return the value at the Nth position ` ` ` `// of the ways array. ` ` ` `return` `ways[(` `int` `)N]; ` ` ` `} ` ` ` ` ` `static` `void` `printArray(` `long` `[] coins) ` ` ` `{ ` ` ` `for` `(` `long` `i : coins) ` ` ` `System.out.println(i); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `long` `Coins[] = { ` `1` `, ` `5` `, ` `10` `}; ` ` ` ` ` `System.out.println(` `"The Coins Array:"` `); ` ` ` `printArray(Coins); ` ` ` ` ` `System.out.println(` `"Solution:"` `); ` ` ` `System.out.println(getNumberOfWays(` `12` `, Coins)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`/* We have input values of N and ` `an array Coins that holds all of ` `the coins. We use data type of ` `long because we want to be able ` `to test large values without ` `integer overflow*/` `using` `System; ` ` ` `public` `class` `getWays ` `{ ` ` ` ` ` `static` `long` `getNumberOfWays(` `long` `N, ` `long` `[] Coins) ` ` ` `{ ` ` ` `// Create the ways array to 1 plus the amount ` ` ` `// to stop overflow ` ` ` `long` `[] ways = ` `new` `long` `[(` `int` `)N + 1]; ` ` ` ` ` `// Set the first way to 1 because its 0 and ` ` ` `// there is 1 way to make 0 with 0 coins ` ` ` `ways[0] = 1; ` ` ` ` ` `// Go through all of the coins ` ` ` `for` `(` `int` `i = 0; i < Coins.Length; i++) ` ` ` `{ ` ` ` ` ` `// Make a comparison to each index value ` ` ` `// of ways with the coin value. ` ` ` `for` `(` `int` `j = 0; j < ways.Length; j++) ` ` ` `{ ` ` ` `if` `(Coins[i] <= j) ` ` ` `{ ` ` ` ` ` `// Update the ways array ` ` ` `ways[j] += ways[(` `int` `)(j - Coins[i])]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// return the value at the Nth position ` ` ` `// of the ways array. ` ` ` `return` `ways[(` `int` `)N]; ` ` ` `} ` ` ` ` ` `static` `void` `printArray(` `long` `[] coins) ` ` ` `{ ` ` ` `foreach` `(` `long` `i ` `in` `coins) ` ` ` `Console.WriteLine(i); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `long` `[]Coins = { 1, 5, 10 }; ` ` ` ` ` `Console.WriteLine(` `"The Coins Array:"` `); ` ` ` `printArray(Coins); ` ` ` ` ` `Console.WriteLine(` `"Solution:"` `); ` ` ` `Console.WriteLine(getNumberOfWays(12, Coins)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

The Coins Array: 1 5 10 Solution: 4

## Recommended Posts:

- How to solve a Dynamic Programming Problem ?
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Vertex Cover Problem | Set 2 (Dynamic Programming Solution for Tree)
- Coin Change | DP-7
- C Program Coin Change
- Python Program for Coin Change
- Java Program for Coin Change
- Dynamic Programming | High-effort vs. Low-effort Tasks Problem
- Dynamic Programming on Trees | Set 2
- Dynamic Programming on Trees | Set-1
- Bitmasking and Dynamic Programming | Set-2 (TSP)
- Dynamic Programming vs Divide-and-Conquer
- Number of Unique BST with a given key | Dynamic Programming
- Top 20 Dynamic Programming Interview Questions
- Double Knapsack | Dynamic Programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.