Skip to main content

The Number of Ways to Make the Sum - Solution & Explanation

MediumPremiumFree on FleetCodeArrayDynamic Programming13 min read
Practice this problem

Problem Statement

You have an infinite number of coins with values 1, 2, and 6, and only 2 coins with value 4.

Given an integer n, return the number of ways to make the sum of n with the coins you have.

Since the answer may be very large, return it modulo 109 + 7.

Note that the order of the coins doesn't matter and [2, 2, 3] is the same as [2, 3, 2].

 

Example 1:

Input: n = 4

Output: 4

Explanation:

Here are the four combinations: [1, 1, 1, 1], [1, 1, 2], [2, 2], [4].

Example 2:

Input: n = 12

Output: 22

Explanation:

Note that [4, 4, 4] is not a valid combination since we cannot use 4 three times.

Example 3:

Input: n = 5

Output: 4

Explanation:

Here are the four combinations: [1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 2, 2], [1, 4].

 

Constraints:

  • 1 <= n <= 105

Approach Overview

Problem Overview: Given an integer n, compute how many different combinations of coins can produce the exact sum. Some coins can be used unlimited times while a special coin (value 4) has a limited count. Order does not matter, so combinations like 1+2+1 and 2+1+1 count as the same. The task reduces to counting valid coin combinations efficiently.

Approach 1: Dynamic Programming (Complete Knapsack) (Time: O(n), Space: O(n))

This approach models the problem as a classic coin change combinations problem using dynamic programming. Create a dp array where dp[i] represents the number of ways to make sum i. Initialize dp[0] = 1 since one way exists to make sum 0. Iterate through the available coin values and update the DP array in increasing order, adding dp[i - coin] to dp[i]. This works because each coin can be reused indefinitely, which matches the complete knapsack pattern. For coins with limited availability, iterate only for the allowed count or treat them carefully so they are not reused beyond their limit.

The key insight: by iterating coins in the outer loop and sums in the inner loop, each combination is counted once regardless of order. The DP array gradually accumulates the number of valid combinations for every intermediate sum up to n.

Approach 2: Preprocessing + Dynamic Programming (Complete Knapsack) (Time: O(n), Space: O(n))

A cleaner optimization separates unlimited coins from the limited coin. First run a complete knapsack DP using only the unlimited coins (for example values 1, 2, and 6). This preprocessing step computes the number of ways to make every sum up to n using only those coins.

Next incorporate the limited coin (value 4) by enumerating how many times it appears. For each valid count of that coin, subtract its contribution from the target sum and add the number of ways stored in the precomputed DP table. Because the DP table already contains the combinations for the remaining sum, the final answer becomes the sum of those possibilities. This split keeps the DP logic simple and avoids repeatedly recalculating states.

Recommended for interviews: The standard dynamic programming complete knapsack approach is what interviewers expect. It demonstrates you recognize the problem as a coin-change combination variant and can implement a correct dp[i] += dp[i - coin] transition. The preprocessing variation shows deeper understanding of how to separate unlimited and limited items in a DP formulation.

Approach 1: Dynamic Programming (Complete Knapsack)

We can start by ignoring coin 4, defining the coin array coins = [1, 2, 6], and then using the idea of the complete knapsack problem. We define f[j] as the number of ways to make up amount j using the first i types of coins, initially f[0] = 1. Then, we iterate through the coin array coins, and for each coin x, we iterate through amounts from x to n, updating f[j] = f[j] + f[j - x].

Finally, f[n] is the number of ways to make up amount n using coins 1, 2, 6. Then, if n geq 4, we consider choosing one coin 4, so the number of ways becomes f[n] + f[n - 4], and if n geq 8, we consider choosing two coins 4, so the number of ways becomes f[n] + f[n - 4] + f[n - 8].

Note the modulus operation for the answer.

The time complexity is O(n), and the space complexity is O(n). Where n is the amount.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 2: Preprocessing + Dynamic Programming (Complete Knapsack)

We can start by preprocessing the number of ways to make up every amount from 1 to 10^5, and then return the corresponding number of ways based on the value of n:

  • If n < 4, directly return f[n];
  • If 4 leq n < 8, return f[n] + f[n - 4];
  • If n geq 8, return f[n] + f[n - 4] + f[n - 8].

Note the modulus operation for the answer.

The time complexity is O(n), and the space complexity is O(n). Where n is the amount.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Dynamic Programming (Complete Knapsack)—
Preprocessing + Dynamic Programming (Complete Knapsack)—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming (Complete Knapsack)O(n)O(n)General case when computing combinations with reusable coins
Preprocessing + DPO(n)O(n)Useful when some coins are unlimited and others have limited counts

Video Solution

3183. The Number of Ways to Make the Sum (Leetcode Medium) • Programming Live with Larry • 479 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is The Number of Ways to Make the Sum easy or hard?
The problem is rated Medium because it requires recognizing the coin change combination pattern and correctly handling unlimited versus limited coins. Once mapped to a complete knapsack DP, the implementation becomes straightforward.
The Number of Ways to Make the Sum Python/Java solution
In both Python and Java, implement a dp array of size n+1 and initialize dp[0] = 1. Loop through each coin and update dp[i] += dp[i - coin] for i from coin to n. The same logic works in C++, Go, and TypeScript because the algorithm only uses arrays and loops.
How to solve The Number of Ways to Make the Sum in O(n)?
Use a 1D dynamic programming array. Initialize dp[0] = 1 and iterate through each coin value. For every sum from coin to n, update dp[i] += dp[i - coin]. This complete knapsack transition accumulates the number of combinations in linear time relative to the target sum.
What is the best approach for The Number of Ways to Make the Sum?
The optimal approach uses dynamic programming with the complete knapsack pattern. Maintain a dp array where dp[i] stores the number of ways to form sum i. For each coin, update dp[i] += dp[i - coin]. This runs in O(n) time and O(n) space while counting combinations without considering order.
Is The Number of Ways to Make the Sum asked at Google/Amazon/Meta?
Coin change and knapsack-style dynamic programming problems frequently appear in interviews at companies like Google, Amazon, and Meta. Variants that count combinations rather than minimum coins are especially common because they test DP state transitions and order handling.
What data structure is used in The Number of Ways to Make the Sum?
The solution mainly relies on a one-dimensional array used for dynamic programming. The dp array stores the number of ways to build each intermediate sum. Iterative updates simulate the complete knapsack process for unlimited coins.
What is the time complexity of The Number of Ways to Make the Sum?
The standard dynamic programming solution runs in O(n) time where n is the target sum. Each coin iterates through the DP array once, and updates are constant time. Space complexity is O(n) for storing the DP table of combination counts.

Ready to solve this problem?

Practice The Number of Ways to Make the Sum with our built-in code editor and test cases.

Practice on FleetCode