Inverse Coin Change - Problem

You are given a 1-indexed integer array numWays, where numWays[i] represents the number of ways to select a total amount i using an infinite supply of some fixed coin denominations.

Each denomination is a positive integer with value at most numWays.length. However, the exact coin denominations have been lost.

Your task is to recover the set of denominations that could have resulted in the given numWays array.

Return a sorted array containing unique integers which represents this set of denominations. If no such set exists, return an empty array.

Input & Output

Example 1 — Basic Case
$ Input: numWays = [1,1,2,2,3,4]
Output: [1,2]
💡 Note: With coins [1,2]: ways to make 1=1 (coin 1), ways to make 2=1 (coin 2), ways to make 3=2 (1+1+1 or 1+2), ways to make 4=2 (1+1+1+1 or 2+2), ways to make 5=3 (add coin 1 or 2 to make 4 or 3), ways to make 6=4 (multiple combinations)
Example 2 — Single Coin
$ Input: numWays = [1,0,0,1]
Output: [1,4]
💡 Note: With coins [1,4]: ways to make 1=1 (coin 1), ways to make 2=0 (impossible), ways to make 3=0 (impossible), ways to make 4=1 (coin 4)
Example 3 — No Valid Set
$ Input: numWays = [1,2,3]
Output: []
💡 Note: No valid coin set can produce this pattern - the growth is too rapid for any reasonable denomination set

Constraints

  • 1 ≤ numWays.length ≤ 1000
  • 0 ≤ numWays[i] ≤ 1000
  • Each denomination value is at most numWays.length

Visualization

Tap to expand
Inverse Coin Change - Greedy Reconstruction INPUT numWays Array (1-indexed) 1 2 3 4 5 6 1 1 2 2 3 4 numWays[i] = ways to make amount i Interpretation: Amount 1: 1 way Amount 2: 1 way Amount 3: 2 ways Amount 4: 2 ways Amount 5: 3 ways Amount 6: 4 ways Goal: Find coin denominations that produce these counts Input: numWays = [1,1,2,2,3,4] ALGORITHM STEPS 1 Initialize Start with empty coins set dp[0] = 1, rest = 0 2 Check Each Amount For i=1 to n, compare dp[i] vs numWays[i] 3 Detect New Coin If numWays[i] > dp[i], i is a denomination 4 Update DP Add coin and recalculate dp[j] += dp[j-coin] Greedy Trace: i=1: dp[1]=0, need=1 --> Add 1 Update: dp=[1,1,1,1,1,1,1] i=2: dp[2]=1, need=1 --> OK i=3: dp[3]=1, need=2 --> Add 2 Update: dp=[1,1,1,2,2,3,4] FINAL RESULT Recovered Denominations: 1 2 Verification: With coins {1, 2}: 1 = [1] ............ 1 way 2 = [2] ............ 1 way 3 = [1+1+1],[1+2] .. 2 ways 4 = [1x4],[1+1+2] .. 2 ways Output: [1, 2] OK - Matches numWays! Sorted unique integers Key Insight: A denomination d exists if and only if numWays[d] > (computed ways without d). The greedy approach simulates coin change DP, detecting when the actual count exceeds our computed count - this signals a new coin at that index. Time: O(n^2), Space: O(n) where n = length of numWays array. TutorialsPoint - Inverse Coin Change | Greedy Reconstruction Approach
Asked in
Google 15 Amazon 12 Microsoft 8
23.0K Views
Medium Frequency
~35 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