Minimum Coins for Making a Given Value in Java

In this section, we are going to learn how one can use minimum coins for making a given value. The problem of making a given value using minimum coins is a variation of coin change problem.

In this problem, a value Y is given. The task is to find the minimum number of coins that is required to make the given value Y. The coins should only be taken from the given array C[] = {C1, C2, C3, C4, C5, …}. If any number of coins is not suitable for making a given value, then display the appropriate message.

Example 1:

Input: C[] = {5, 10, 25}, Y = 30

Output: Minimum of 2 coins are required to make the sum 30.

Explanation: There can be various combinations of coins for making the sum 30.

5 + 5 + 5 + 5 + 5 + 5 = 30 (total coins: 6)

5 + 5 + 5 + 5 + 10 = 30 (total coins: 5)

5 + 5 + 10 + 10 = 30 (total coins: 4)

10 + 10 + 10 = 30 (total coins: 3)

5 + 25 = 30 (total coins: 2)

Thus, we see that at least 2 coins are required to make the sum 30. Hence, our answer is 30.

Example 2:

Input:

C[] = {4, 3, 2, 6}, Y = 15

Output: Minimum of 3 coins are required to make the sum 15.

Explanation: There can be various combinations of coins for making the sum 12.

2 + 2 + 2 + 2 + 2 + 2 + 3 = 15 (total coins: 7)

2 + 2 + 2 + 3 + 3 + 3 = 15 (total coins: 6)

2 + 2 + 2 + 2 + 3 + 4 = 15 (total coins: 6)

2 + 2 + 2 + 3 + 6 = 15 (total coins: 5)

3 + 3 + 3 + 3 + 3 = 15 (total coins: 5)

2 + 2 + 3 + 4 + 4 = 15 (total coins: 5)

3 + 3 + 3 + 6 = 15 (total coins: 4)

4 + 4 + 4 + 3 = 15 (total coins: 4)

2 + 3 + 4 + 6 = 15 (total coins: 4)

4 + 4 + 4 + 3 = 15 (total coins: 4)

6 + 6 + 3 = 15 (total coins: 3)

Thus, we see that at least 3 coins are required to make the sum 15. Hence, our answer is 3.

Solution to the problem

The problem can be solved in various ways. But in this section, we will solve the problem by using the following two approaches:

  • Using Recursion
  • Using Dynamic Programming

Let's see the approaches one by one.

Using Recursion

Using the following recursive formula one can solve the given problem.

Observe the following implementation.

FileName: MinCoins.java

Output:

For the sum 30
The minimum number of required coins is: 2
Using the following coins: 
5 10 25 

For the sum 15
The minimum number of required coins is: 3
Using the following coins: 
4 3 2 6

Time Complexity: The time complexity of the above program is exponential.

As exponential time complexity is always high, therefore, it is required to reduce the time complexity. Thus, we need an optimized approach. The following implementation shows the same.

Using Dynamic Programming

If we observe the above approach, there are many subproblems that have been solved more than one time. So, the optimization is straightforward. We have to remove the extra computation time of the redundant subproblems. We will be doing the same in the following program.

FileName: MinCoins1.java

Output:

For the sum 30
The minimum number of required coins is: 2
Using the following coins: 
5 10 25 

For the sum 15
The minimum number of required coins is: 3
Using the following coins: 
4 3 2 6

Time Complexity: The time complexity of the above program is mainly dependent on the nested for loop that is present in the method minNoCoins(). As the inner for loop runs from 0 to s, and the outer loop runs from 1 to Y, therefore, the time complexity is O(sY), where Y is the given value and s is the size of the coins array.