Inverse Coin Change - Problem

Imagine you're a cryptographer who has intercepted a secret message! You've found an array numWays that contains clues about a hidden coin system. Each position i tells you how many ways you can make amount i using some mysterious coin denominations.

Your mission: reverse-engineer the original coin denominations!

The Challenge: Given a 1-indexed array numWays[i] representing the number of ways to make amount i, determine what coin denominations could have produced this pattern. The coins have infinite supply, and each denomination is a positive integer ≤ numWays.length.

Goal: Return a sorted array of unique coin denominations that could generate the given numWays array. If no valid set exists, return an empty array.

Example: If numWays = [0, 1, 1, 2], this means:
• 1 way to make amount 1
• 1 way to make amount 2
• 2 ways to make amount 3
This could come from coins [1, 2]!

Input & Output

example_1.py — Basic Case
$ Input: numWays = [0, 1, 1, 2]
Output: [1, 2]
💡 Note: With coins [1, 2]: amount 1 has 1 way (use coin 1), amount 2 has 1 way (use coin 2), amount 3 has 2 ways (1+2 or 1+1+1). This matches the given numWays pattern.
example_2.py — Single Coin
$ Input: numWays = [0, 1, 1, 1]
Output: [1]
💡 Note: Only coin denomination 1 can produce this pattern: 1 way each to make amounts 1, 2, and 3 (using 1, 2, and 3 coins of denomination 1 respectively).
example_3.py — No Valid Coins
$ Input: numWays = [0, 0, 1]
Output: []
💡 Note: This pattern is impossible. If we can make amount 2 in 1 way, we must be able to make smaller amounts, but numWays[1] = 0 indicates amount 1 cannot be made.

Constraints

  • 1 ≤ numWays.length ≤ 100
  • 0 ≤ numWays[i] ≤ 1000
  • numWays[0] = 0 (always, as there's only one way to make 0: use no coins)
  • Each coin denomination is a positive integer ≤ numWays.length

Visualization

Tap to expand
🕵️ Coin Detective: Solving the MysteryEvidence FoundnumWays: [0,1,1,2]Payment patterns recorded1Coin Found!2Coin Found!Solution: [1, 2]Mystery Solved!✓ Pattern matches perfectlyVerification Steps:Amount 1: With coins [1] → 1 way (1) ✓ matches numWays[1]=1Amount 2: With coins [1] → 1 way (1+1) ✓ matches numWays[2]=1Amount 3: With coins [1] → 1 way (1+1+1) ✗ expected 2 waysAdd coin 2: Amount 3 → 2 ways (1+1+1, 2+1) ✓ matches!Final check: All amounts match the evidence pattern🎯 Case Closed: Coins [1, 2] recovered successfully!💡 Key Insight: If numWays[i] > 0 but we can't make amount i with current coins, then i is a coin!This reverse-engineering approach builds the solution incrementally by testing each amount
Understanding the Visualization
1
Examine the Evidence
numWays = [0, 1, 1, 2] tells us: 1 way for amount 1, 1 way for amount 2, 2 ways for amount 3
2
Start with Amount 1
Since amount 1 has exactly 1 way, coin denomination 1 must exist
3
Check Amount 2
With coin 1, we can make amount 2 in 1 way (1+1). This matches numWays[2]=1
4
Analyze Amount 3
With coin 1, we can make amount 3 in 1 way (1+1+1). But numWays[3]=2! We need another coin
5
Discover Coin 2
Adding coin 2 gives us 2 ways for amount 3: (1+1+1) and (2+1). Perfect match!
Key Takeaway
🎯 Key Insight: The detective method works because each coin denomination reveals itself when we can't make its amount with previously discovered coins. By incrementally building our coin set and verifying against the evidence, we can reconstruct the original currency system with confidence.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
43.7K Views
Medium Frequency
~25 min Avg. Time
1.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