×

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming is a popular algorithm method to solve optimization problems by breaking them into easier subproblems.

We will generate java programs to understand our topic practically.

Introduction to Coin Change Problem

Problem statement

An array of coins with differing values and an integer sum is provided.

It is asked to generate a combination of minimum possible coins to reach the given integer sum. If in case that sum cannot be reached, just return -1.

Now, let us try to understand the coin change problem through examples.

Example 1:

Array 1: {5, 7, 9, 12, 15, 35, 45};

There are 7 array elements present inside the above array. These array elements serve as the value of coins. The coin change problem aims to collect a certain amount of money using the minimum possible number of coins.

It is to be noted that the given values are associated with an infinite number of coins. In other words, you are allowed to use these values any number of times you require.

Example 2:

Array 2: {1, 2, 4, 6, 7, 12, 15, 16};

Required Sum: 8

The coins needed to reach the goal amount i.e 8: {2, 6}

Thus, to reach the goal amount, only two coins are required in the dynamic programming technique.

Now, let us see it for the greedy algorithm.

The coins needed to reach the goal amount i.e 8: {2, 2, 4}

In this case, the required number of coins is three. This is because the latter does local optimization.

Example 3:

Array 3: {2, 6, 10, 12, 14, 16};

Required Sum: 17

Output: -1

There are no possible combinations to get the sum as 17. Thus, the output is 17.

Solutions to the Coin Change Problem

Two solutions to the Coin Change Problem exist–

  1. Recursion - Simple and slow approach.
  2. Dynamic Programming – A judicious and competent approach

Now, let us understand the recursive approach and its limitations which led to another solution to the same problem which is the development of a dynamic programming approach.

A recursive approach to solving Coin Change

Let the array of coins be

  • coins[] = {1, 2, 3}
  • sum = 4

Two operations can be performed on these coins- they can either be included or excluded.

When the coin is included, its value is added to the current sum solution, that is

(sol + coins [i], I, and if the resultant value is not equal to the target sum, you pass to the subsequent coin, i.e., the subsequent recursive call solution (sol, i++).

Implementation:

File name – Coin.java

// Recursive approach for
// coin change problem in java.
import java.util.*;
class Coin {
    // Returns the count of ways we can
    // sum coins[0...n-1] coins to get sum "sum"
    static int no_of_coins(int coins[], int num, int sum)
    {
        // If the sum is 0 then only 1 solution exists
        // (do not include any coin)
        if (sum == 0)
            return 1;
        // If the sum is less than 0 then no
        // solution exists
        if (sum < 0)
            return 0;
 
        // If no coins are there and the sum
        // is greater than 0, then no
        // solution exist
        if (num <= 0)
            return 0;
 
        // count variable denotes the sum of solutions 
        return no_of_coins(coins, num - 1, sum)
            + no_of_coins(coins, num, sum - coins[num - 1]);
    }
    // Driver code
    public static void main(String args[])
    {
        int coins[] = { 1, 2, 3, 5, 7, 8 };
        int num = coins.length;
        System.out.println("Total number of solutions are: ");
        System.out.println(no_of_coins(coins, num, 6));
    }
}

Output:

Coin Change Problem in dynamic programming

The above recursive method causes the algorithm to compute the same subproblems several times. Therefore, in order to solve the coin change problem proficiently, the Dynamic Programming approach needs to be taken into account.

Coin Change Problem Solution Using Dynamic Programming approach

Implementation:

File name – CoinChangeProblem.java

/* Dynamic Programming approach to solving Coin
Change problem in java*/
import java.util.Arrays;
class CoinChangeProblem {
	static long count(int coins[], int num, int sum)
	{
		long[] table1 = new long[sum + 1];
		// let all the table values be initialized by 0
		Arrays.fill(table1, 0);
		// Base case (when the given value is 0)
		table1[0] = 1;
		// Traverse the coins one by one and keep updating the table[]
		// values after the index greater than or equal to
		// the value of the picked coin
		for (int j = 0; j < num; j++)
			for (int k = coins[j]; k <= sum; k++)
				table1[k] += table1[k - coins[j]];


		return table1[sum];
	}
	// Driver Function to test the above function
	public static void main(String args[])
	{
		int coins[] = { 1, 2, 3, 5, 7, 8  };
		int num = coins.length;
		int sum = 6 ;
		System.out.println("Total number of solutions are: ");
		System.out.println(count(coins, num, sum));
	}
}

Output:

Coin Change Problem in dynamic programming

The time complexity of this function: O(num * sum)

The space Complexity of this function: O(sum)

Space optimized solution

Implementation:

The following steps need to be followed to implement the idea:

  • Initialize the linear array table with zeroes.
  • With sum = 0, there is a way.
  • Update the level-wise quantity of ways of coin till the ith coin.
  • Keep solving until j <= sum.

File name - CoinChangeProblem.java

/* Dynamic Programming approach to solving Coin
Change problem in java*/
import java.util.Arrays;


class CoinChangeProblem {
	static long count(int coins[], int num, int sum)
{
	// table[i] will store the number of solutions
	
	int table1[] = new int[sum + 1];


	// Base case (If the given value is 0)
	table1[0] = 1;


	// traverse all coins one by one and keep updating the table[]
	
	for (int j = 0; j < num; j++)
		for (int k = coins[j]; k <= sum; k++)
			table1[k] += table1[k - coins[j]];


	return table1[sum];
}


	// Driver Function to check the working of the above function
	public static void main(String args[])
	{
		int coins[] = { 1, 2, 3, 5, 7, 8  };
		int num = coins.length;
		int sum = 6;
		System.out.println("The total number of solutions are: ");
		System.out.println(count(coins, num, sum));
	}
}


Output:

Coin Change Problem in dynamic programming

Time Complexity: O(num*sum)
Space Complexity: O(sum)

Coin change using the Top Down (Memoization) Dynamic Programming:

Implementation:

The following steps need to be followed to implement the idea:

  • Store the Overlapping subproblems by means of a 2-D vector.
  • Pass through the entire array to find the solution and store it in the memorization table.
  • Use the memorization table to get the optimal solution.

File name - CoinChangeProblem.java

/* Dynamic Programming approach to solving Coin
Change problem in java*/


import java.util.*;


class CoinChangeProblem {


	static int coin(int[] ar, int v1, int n1, int[][] dp1)
	{
		if (v1 == 0)
			return dp1[n1][v1] = 1;
		if (n1 == 0)
			return 0;
		if (dp1[n1][v1] != -1)
			return dp1[n1][v1];
		if (ar[n1 - 1] <= v1) {


			// Either choose this coin or don’t 
			return dp1[n1][v1]
				= coin(ar, v1 - ar[n1 - 1], n1, dp1)
				+ coin(ar, v1, n1 - 1, dp1);
		}
		else //  discard this coin
			return dp1[n1][v1] = coin(ar, v1, n1 - 1, dp1);
	}


	// Driver code to check the working of the above function
	public static void main(String[] args)
	{
		int tc1 = 1;
		while (tc1 != 0) {
			int n1, v1;
			n1 = 6;
			v1 = 6;
			int[] ar = { 1, 2, 3, 5, 7, 8 };
			int[][] dp = new int[n1 + 1][v1 + 1];
			for (int[] row : dp)
				Arrays.fill(row, -1);
			int ans = coin(ar, v1, n1, dp);
			System.out.println("The total solution are: ");
			System.out.println(ans);
			tc1--;
		}
	}
}




Output:

Coin Change Problem in dynamic programming

Time complexity: O(num*sum)
Space complexity: O(num*sum)

Applications of Coin Change Problem

This algorithm can be efficiently used to distribute change,

for example, in a soda vending machine that accepts bills and coins and distributes coins.

Buying a 60-cent soda pop with a dollar is one example. This leaves 40 cents to change, or in the United States, one quarter, one dime, and one nickel for the smallest coin pay. However, if the nickel tube were empty, the machine would distribute four dimes.


Related Topics

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.