Minimum Number of Coins to be Added - Problem
Problem: Imagine you're a coin collector trying to build a complete set! You have an array of coins with different values, and you want to be able to make every amount from 1 to target using combinations of these coins.

Your goal is to find the minimum number of coins you need to add (of any denomination you choose) so that you can form any integer value in the range [1, target].

Key Points:
• You can use any coin multiple times
• You can choose what value coins to add
• A subsequence means any combination of coins that sum to the desired amount

Example: If coins = [1, 3] and target = 6, you can make 1, 3, 4 (1+3), but you cannot make 2, 5, or 6. You'd need to add coins to cover all missing values.

Input & Output

example_1.py — Basic case
$ Input: coins = [1, 3], target = 6
Output: 1
💡 Note: With coins [1, 3] we can form sums: 1, 3, 4 (1+3). We cannot form 2, 5, or 6. Adding one coin of value 2 allows us to form all values 1-6: [1], [2], [3], [1+3=4], [2+3=5], [1+2+3=6].
example_2.py — No coins needed
$ Input: coins = [1, 1, 1], target = 3
Output: 0
💡 Note: With three coins of value 1, we can form: 1 (one coin), 2 (two coins), 3 (three coins). All values from 1 to 3 are already obtainable, so no additional coins needed.
example_3.py — Multiple gaps
$ Input: coins = [1, 5, 10], target = 20
Output: 2
💡 Note: We can form 1, 5, 6, 10, 11, 15, 16. Missing many values like 2, 3, 4, 7, 8, 9, etc. The greedy algorithm will add coins optimally to cover all gaps with minimum additions.

Constraints

  • 1 ≤ coins.length ≤ 105
  • 1 ≤ coins[i] ≤ 104
  • 1 ≤ target ≤ 2 × 105
  • All coin values are positive integers

Visualization

Tap to expand
Coin Addition Strategy VisualizationTarget: Form all amounts 1 to 6Available coins: [1, 3]Step 1: What can we form?123456Green = Can form, Red = Cannot formStep 2: Greedy algorithm in actionmaxReach = 0 → Process coin 1: maxReach = 1maxReach = 1 → Process coin 3: 3 > 1+1, GAP!Add coin 2: maxReach = 1+2 = 3Now coin 3: 3 ≤ 3+1 ✓, maxReach = 3+3 = 6Step 3: Final result123456Added coins: [2] → Final coins: [1, 2, 3]🎯 **Key Insight:** Add coin of value (maxReach + 1) when gaps are found for optimal coverage
Understanding the Visualization
1
Sort coins by value
Process smaller coins first to build foundation
2
Track maximum reachable sum
Keep track of highest sum we can currently form
3
Extend reach with available coins
Use coins that don't create gaps in our sequence
4
Fill gaps strategically
Add optimal coin values when gaps are discovered
Key Takeaway
🎯 Key Insight: The greedy approach works because when we can form all amounts up to X and find a gap, adding coin value X+1 optimally extends our reach while minimizing total additions.
Asked in
Google 42 Amazon 38 Meta 28 Microsoft 25
78.5K 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