• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Solve the Coin Change Problem

laptop infographic online business 6087062

Introduction

The Coin Change Problem is a classic dynamic programming problem in computer science. It challenges us to determine the number of ways to make a specific amount using a given set of coin denominations, or to find the minimum number of coins needed. This problem is widely used to understand recursion, memoization, and bottom-up dynamic programming techniques.

Problem Statement

Given an array of coin denominations and a total amount, determine the minimum number of coins required to make that amount. If it’s not possible to make the amount using the given coins, return -1.

Approach

A dynamic programming solution is typically used to solve this problem efficiently. We create an array where each index represents the minimum number of coins needed to form that amount. Starting from 0 up to the target amount, we build our solution incrementally. This bottom-up strategy ensures that every sub-problem is solved only once and stored for future use.

Java Implementation

Here is an implementation of the minimum coin change solution in Java using a bottom-up dynamic programming approach:

public class CoinChange {

    public static int minimumCoins(int[] coins, int amountRequired) {
        int max = amountRequired + 1;
        int[] dbArr = new int[amountRequired + 1];

        for (int i = 0; i <= amountRequired; i++) {
            dbArr[i] = max;
        }
        dbArr[0] = 0;

        for (int coin : coins) {
            for (int i = coin; i <= amountRequired; i++) {
                dbArr[i] = Math.min(dbArr[i], dbArr[i - coin] + 1);
            }
        }

        return dbArr[amountRequired] > amountRequired ? -1 : dbArr[amountRequired];
    }

    public static void main(String[] args) {
        int[] coins = {1, 2, 5};
        int amount = 11;

        int result = minimumCoins(coins, amount);
        if (result != -1) {
            System.out.println("Minimum coins required: " + result);
        } else {
            System.out.println("Amount cannot be formed with given coins.");
        }
    }
}

Example Explanation

In the given example, we use the coin denominations {1, 2, 5} to make the amount 11. The optimal solution uses three coins: 5 + 5 + 1, so the program outputs 3. The algorithm tests every amount up to 11 and chooses the combination with the fewest coins at each step.

Time and Space Complexity

The time complexity of this algorithm is O(n * amount), where n is the number of coin denominations. The space complexity is O(amount) due to the single-dimensional dp array used to store subproblem results.

Conclusion

The Coin Change Problem illustrates the power of dynamic programming in solving optimization problems. By breaking the problem into smaller subproblems and building up the solution, we can efficiently compute the result, even for larger inputs. This Java implementation provides a clear and effective solution to finding the minimum number of coins required to reach a given amount.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • How AI Coding Assistants Are Reshaping Software Engineering Careers
  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring

TAGS

AI Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions