The Number of Ways to Make the Sum - Problem

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 10⁹ + 7.

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

Input & Output

Example 1 — Small Sum
$ Input: n = 8
Output: 21
💡 Note: Multiple ways to make 8: using coins {1,1,6}, {2,2,2,2}, {1,1,2,4}, {4,4}, etc. Total of 21 different combinations.
Example 2 — Minimum Case
$ Input: n = 1
Output: 1
💡 Note: Only one way to make sum 1: use a single coin of value 1.
Example 3 — Medium Sum
$ Input: n = 4
Output: 5
💡 Note: Ways to make 4: {1,1,1,1}, {1,1,2}, {2,2}, {4}. Note we can use one coin-4 from our limited supply.

Constraints

  • 1 ≤ n ≤ 104
  • Answer modulo 109 + 7

Visualization

Tap to expand
Making Sum = 8 with Available CoinsUnlimited Coins126Limited Coins44only 2Example Ways to Make Sum = 8{1,1,6} {2,2,2,2} {1,1,2,4}{4,4} {1,1,1,1,1,1,2}{2,6} ...Total: 21 different ways
Understanding the Visualization
1
Available Coins
Unlimited: 1,2,6 coins. Limited: exactly 2 coins of value 4
2
State Tracking
Track (remaining_sum, coin4_used) to avoid overcounting
3
Combine Results
Sum all ways using 0, 1, or 2 coin-4s
Key Takeaway
🎯 Key Insight: Track limited coin usage as part of DP state to handle mixed constraints correctly
Asked in
Google 25 Amazon 20 Microsoft 15
23.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen