Minimum Number of Coins to be Added - Problem
You are given a 0-indexed integer array coins, representing the values of the coins available, and an integer target.
An integer x is obtainable if there exists a subsequence of coins that sums to x.
Return the minimum number of coins of any value that need to be added to the array so that every integer in the range [1, target] is obtainable.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Input & Output
Example 1 — Basic Case
$
Input:
coins = [1,3], target = 6
›
Output:
1
💡 Note:
We can make sums 1, 3, 4 initially. Missing: 2, 5, 6. Add coin 2 to make all sums 1-6 possible: 1, 2, 3, 4(1+3), 5(2+3), 6(1+2+3).
Example 2 — Already Complete
$
Input:
coins = [1,2,3], target = 7
›
Output:
1
💡 Note:
With coins 1,2,3 we can make sums 1,2,3,4(1+3),5(2+3),6(1+2+3). To make 7, we need to add one more coin of value 4, giving us 7(3+4). So 1 addition is needed.
Example 3 — Large Gap
$
Input:
coins = [1,5,10], target = 20
›
Output:
2
💡 Note:
Can make 1, 5, 6, 10, 11, 15, 16. Missing many sums. Need to add coins 2 and 4 to fill gaps efficiently.
Constraints
- 1 ≤ coins.length ≤ 105
- 1 ≤ coins[i] ≤ 104
- 1 ≤ target ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code