Skip to content

ORIGINAL CODING QUESTION

Minimum practice tokens

You have unlimited tokens of each given positive value. Return the fewest tokens needed to total exactly the target, or -1 if impossible.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n token values, then target. Print the minimum count.

Constraints

1 ≤ n ≤ 100; 1 ≤ token value ≤ 10,000; 0 ≤ target ≤ 10,000.

Example 1

Input

3
1 3 4
6

Output

2

Two value-3 tokens work; greedy 4 + 1 + 1 uses more.

Example 2

Input

1
2
3

Output

-1

Odd totals are impossible using only value-2 tokens.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Choosing the largest available value is not always optimal.

Hint 2

Let dp[s] be the minimum count to reach sum s.

Hint 3

Set dp[0] = 0; minimize dp[s - token] + 1 over valid token values.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →