Dynamic Programming
Coin Change Problem
Combinatorics
Algorithms
Mathematics

Number of ways to make change for amount N

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The problem of determining the number of ways to make change for a given amount `N` using a set of coin denominations is a classic problem in computer science and mathematics. This problem not only tests one's understanding of combinatorics but also highlights efficient algorithmic solutions such as dynamic programming.

Problem Definition

Given an integer `N` and a set of coin denominations `C = {c1, c2, ..., cm}`, determine the number of distinct ways to make change for the amount `N`. Each denomination can be used multiple times.

Dynamic Programming Approach

The most efficient approach to solve this problem is by using dynamic programming. Let's break down the process:

Initialization

  1. Define the Problem: Let `dp[i]` represent the number of ways to make change for the amount `i` using the available denominations.
  2. Base Case: There is one way to make change for zero amount: use no coins. Hence, `dp[0] = 1`.

Recurrence Relation

For each coin `c` in the set of denominations, and for every amount from `c` to `N`, update the number of ways to make change. The recurrence relation is as follows:

• For each coin: `dp[i] = dp[i] + dp[i - c]`

Intuitively, this means the number of ways to make change for the amount `i` is equal to its previous value plus the number of ways to make change for the amount `i - c` (i.e., including the use of the current coin).

Step-by-step Calculation

  1. Initialization: • `dp[0] = 1`
  2. Iterate over each coin: • For each coin `c` in denominations: • Update `dp[i]` from `c` to `N`: • `dp[i] = dp[i] + dp[i - c]`

Example

Suppose we need to make change for `N = 4` with denominations `C = {1, 2, 3}`.

Initialize: `dp = [1, 0, 0, 0, 0]`, corresponding to amounts `[0, 1, 2, 3, 4]`. • Using coin `1`: • Update dp: `[1, 1, 1, 1, 1]` • Using coin `2`: • Update dp: `[1, 1, 2, 2, 3]` • Using coin `3`: • Update dp: `[1, 1, 2, 3, 4]`

Thus, there are four ways to make change for the amount `4`: `{1, 1, 1, 1}`, `{1, 1, 2}`, `{2, 2}`, and `{1, 3}`.

Mathematical Analysis

The time complexity of this approach is `O(m * N)`, where `m` is the number of denominations and `N` is the target amount. The space complexity is `O(N)` due to the storage requirement for the `dp` array.

Summary Table

The following table summarizes the dynamic programming approach to solving the number of ways to make change problem:

AspectDetails
Problem DefinitionDetermine number of distinct ways to make change for amount N using available denominations.
ApproachDynamic Programming
Base Casedp\[0] = 1
Recurrence Relationdp\[i] = dp\[i] + dp\[i - c]
ComplexityTime: O(m \* N) Space: O(N)
Example4 ways for N = 4 with C = \{1, 2, 3\}: \{1, 1, 1, 1\}, \{1, 1, 2\}, \{2, 2\}, \{1, 3\}

Extensions and Variants

Several variants and extensions of the problem make it even more interesting and applicable to real-world scenarios:

  1. Limited Coin Supply: What if each coin has a limited supply? This variation involves updating logic to ensure the remaining supply is considered.
  2. Minimum Number of Coins: Determine not just the number of ways, but the way using the fewest coins possible.
  3. Permutation Solutions: Instead of combinations, if the order of coins matters, the solutions count can differ.

Conclusion

The coins change problem is a fundamental algorithmic challenge that illustrates the power and efficiency of dynamic programming. By understanding this approach, one can handle more complex variations and deepen their computational thinking in combinatorial problems.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.