Combination Sum IV - Problem

Imagine you're a cashier with an unlimited supply of different coin denominations, and you need to find out how many different ways you can make change for a specific amount. This is exactly what the Combination Sum IV problem asks!

Given an array of distinct positive integers nums and a target integer target, return the number of possible combinations that add up to the target. Unlike traditional combination problems, order matters here - so [1,2] and [2,1] are considered different combinations.

Example: If nums = [1,2,3] and target = 4, the valid combinations are: [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3], [3,1] - that's 7 different ways!

The challenge is to count all these combinations efficiently without actually generating them, as the number can be very large.

Input & Output

example_1.py โ€” Basic case
$ Input: nums = [1,2,3], target = 4
โ€บ Output: 7
๐Ÿ’ก Note: The possible combinations are: [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3], [3,1]. Note that different sequences are counted as different combinations.
example_2.py โ€” Single element
$ Input: nums = [9], target = 3
โ€บ Output: 0
๐Ÿ’ก Note: Since 9 > 3, there's no way to make sum 3 using only 9s, so the answer is 0.
example_3.py โ€” Edge case
$ Input: nums = [1,2], target = 3
โ€บ Output: 3
๐Ÿ’ก Note: The possible combinations are: [1,1,1], [1,2], [2,1]. Total of 3 different ways.

Constraints

  • 1 โ‰ค nums.length โ‰ค 200
  • 1 โ‰ค nums[i] โ‰ค 1000
  • All elements of nums are unique
  • 1 โ‰ค target โ‰ค 1000
  • The number of possible combinations that add up to target is guaranteed to fit in a 32-bit integer

Visualization

Tap to expand
๐ŸŽฐ The Smart Vending MachineCHANGE CALCULATOR$1$2$3Target: $4Ways to make $4:[$1,$1,$1,$1][$1,$1,$2][$1,$2,$1][$2,$1,$1][$2,$2][$1,$3], [$3,$1]TOTAL: 7DP Table Evolution:Step 1: dp = [1, 0, 0, 0, 0]Step 2: dp = [1, 1, 0, 0, 0] (add $1)Step 3: dp = [1, 1, 2, 0, 0] (add $2)Step 4: dp = [1, 1, 2, 4, 0] (add $3)Step 5: dp = [1, 1, 2, 4, 7] (final)Formula: dp[i] += dp[i - coin]For each coin, add ways to make(current_amount - coin_value)Key Insight:๐Ÿ”„ Order matters: [1,2] โ‰  [2,1]โšก Time: O(target ร— coins), Space: O(target)๐Ÿ’ก Build solution bottom-up from smaller amounts
Understanding the Visualization
1
Initialize
Start with dp[0] = 1 (one way to make $0: use no coins)
2
Build Up
For each amount, count ways by adding each coin denomination to smaller amounts
3
Sequence Matters
Unlike traditional coin change, [quarter, dime] and [dime, quarter] are different sequences
Key Takeaway
๐ŸŽฏ Key Insight: Use dp[i] to store ways to make amount i, then dp[i] = sum of dp[i-coin] for all valid coins. This builds the solution efficiently from smaller subproblems.
Asked in
Google 28 Meta 22 Amazon 35 Microsoft 18
94.5K Views
High Frequency
~18 min Avg. Time
2.8K 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