#2952
Medium Algorithms Minimum number of coins to be added
Array Greedy Sorting
58.0% acceptance
Feb 25, 2026
432
65
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn minimum_added_coins(mut coins: Vec<i32>, target: i32) -> i32 {
coins.sort_unstable();
let mut reach = 0i32; // can obtain every integer in [1, reach]
let mut added = 0;
let mut i = 0;
while reach < target {
if i < coins.len() && coins[i] <= reach + 1 {
reach += coins[i];
i += 1;
} else {
// Add coin reach+1
reach = 2 * reach + 1;
added += 1;
}
}
added
}
}