Inverse Coin Change

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.

Example 1
InputnumWays = [0,1,0,2,0,3,0,4,0,5]
Output[2,4,6]
The denominations [2, 4, 6] produce exactly the given number of ways for amounts 1 through 10.
Example 2
InputnumWays = [1,2,2,3,4]
Output[1,2,5]
The denominations [1, 2, 5] produce exactly the given number of ways for amounts 1 through 5.

Constraints

  • 1 <= numWays.length <= 100
  • 0 <= numWays[i] <= 2 * 10^8

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate