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.
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
- Define the Problem: Let `dp[i]` represent the number of ways to make change for the amount `i` using the available denominations.
- 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
- Initialization: • `dp[0] = 1`
- 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:
| Aspect | Details |
| Problem Definition | Determine number of distinct ways to make change for amount N using available denominations. |
| Approach | Dynamic Programming |
| Base Case | dp\[0] = 1 |
| Recurrence Relation | dp\[i] = dp\[i] + dp\[i - c] |
| Complexity | Time: O(m \* N) Space: O(N) |
| Example | 4 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:
- Limited Coin Supply: What if each coin has a limited supply? This variation involves updating logic to ensure the remaining supply is considered.
- Minimum Number of Coins: Determine not just the number of ways, but the way using the fewest coins possible.
- 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
- Numpy argsort - what is it doing?
- O1 algorithm to determine if node is descendant of another node in a multiway tree?
- Obtain forest out of tree with even number of nodes
- Obtaining a powerset of a set in Java
- Number of Zeros in the binary representation of an Integer
- Occlusion algorithms collection
- OCR error correction algorithms
- Ok to have stack depth linearly proportional to some input size?

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 courseTrack 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.