Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: coins = [1,4,10], target = 19 Output: 2 Explanation: We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
Example 2:
Input: coins = [1,4,10,5,7,19], target = 19 Output: 1 Explanation: We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
Example 3:
Input: coins = [1,1,1], target = 20 Output: 3 Explanation: We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16]. It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
Constraints:
1 <= target <= 1051 <= coins.length <= 1051 <= coins[i] <= targetProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,4,10] 19
[1,4,10,5,7,19] 19
[1,1,1] 20
coin-change)most-expensive-item-that-can-not-be-bought)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2952: Minimum Number of Coins to be Added
class Solution {
public int minimumAddedCoins(int[] coins, int target) {
Arrays.sort(coins);
int ans = 0;
for (int i = 0, s = 1; s <= target;) {
if (i < coins.length && coins[i] <= s) {
s += coins[i++];
} else {
s <<= 1;
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2952: Minimum Number of Coins to be Added
func minimumAddedCoins(coins []int, target int) (ans int) {
slices.Sort(coins)
for i, s := 0, 1; s <= target; {
if i < len(coins) && coins[i] <= s {
s += coins[i]
i++
} else {
s <<= 1
ans++
}
}
return
}
# Accepted solution for LeetCode #2952: Minimum Number of Coins to be Added
class Solution:
def minimumAddedCoins(self, coins: List[int], target: int) -> int:
coins.sort()
s = 1
ans = i = 0
while s <= target:
if i < len(coins) and coins[i] <= s:
s += coins[i]
i += 1
else:
s <<= 1
ans += 1
return ans
// Accepted solution for LeetCode #2952: Minimum Number of Coins to be Added
/**
* [2952] Minimum Number of Coins to be Added
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn minimum_added_coins(coins: Vec<i32>, target: i32) -> i32 {
let mut coins = coins;
coins.sort_unstable();
let mut result = 0;
let mut i = 0;
let mut x = 1;
while x <= target {
if i < coins.len() && coins[i] <= x {
x += coins[i];
i += 1;
} else {
x = x * 2;
result += 1;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2952() {
assert_eq!(2, Solution::minimum_added_coins(vec![1, 4, 10], 19));
assert_eq!(
0,
Solution::minimum_added_coins(vec![1, 2, 4, 6, 7, 9, 9, 10], 48)
);
}
}
// Accepted solution for LeetCode #2952: Minimum Number of Coins to be Added
function minimumAddedCoins(coins: number[], target: number): number {
coins.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, s = 1; s <= target; ) {
if (i < coins.length && coins[i] <= s) {
s += coins[i++];
} else {
s <<= 1;
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.