The Indian Engineer

Problem 2952 Minimum Number of Coins to be Added

Posted on 7 mins

Array Greedy Sorting

Problem Statement

Link - Problem 2952

Question

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:

Solution

class Solution {
public:
    int minimumAddedCoins(vector<int>& coins, int target) {
        sort(coins.begin(),coins.end());
        int missing = 1, result = 0, index = 0;
        while(missing<=target)
        {
            if(index<coins.size() && coins[index]<=missing)
            {
                missing += coins[index];
                index++;
            }
            else
            {
                missing += missing;
                result++;
            }
        }
        return result;

    }
};

Complexity Analysis

| Algorithm        | Time Complexity | Space Complexity |
| ---------------- | --------------- | ---------------- |
| Greedy Algorithm | O(n log n)      | O(1)             |

Explanation

Intial Thoughts

To start, we need to understand the concept of obtainable integers and subsequences of the given array. We should think about how the array can be modified to make all integers in the target range obtainable. We can begin by considering the minimum value that needs to be added to the array and how this addition can affect the obtainability of integers in the target range. Some key points to consider include:

  1. Understanding the definition of an obtainable integer and its relation to the given array,
  2. Identifying the role of the target value in determining the obtainability of integers,
  3. Recognizing the need to add coins to the array to achieve obtainability,
  4. Analyzing the importance of the starting value of 1 and how to proceed from there,
  5. Developing a strategy to determine the minimum number of coins to be added

Intuitive Analysis

A more in-depth analysis involves considering the pattern of how numbers become obtainable when coins are added to the array. The given solution involves sorting the array and tracking the ‘missing’ value, which represents the smallest number that cannot be obtained. Some key intuitive steps include:

  1. Sorting the array to understand the available coin values,
  2. Iterating through the sorted array and identifying the ‘missing’ value,
  3. Deciding whether to use an existing coin or add a new coin based on the ‘missing’ value,
  4. Updating the ‘missing’ value accordingly and tracking the number of added coins,
  5. Continuing this process until the ‘missing’ value exceeds the target, thereby ensuring all integers in the range are obtainable

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Sort the coins array and maintain the smallest sum that is unobtainable by induction.

If we don’t use any coins, the smallest integer that we cannot obtain by sum is 1. Suppose currently, for a fixed set of the first several coins the smallest integer that we cannot obtain is x + 1, namely we can form all integers in the range [1, x] but not x + 1.

If the next unused coin’s value is NOT x + 1 (note the array is sorted), we have to add x + 1 to the array. After this addition, we can form all values from x + 1 to 2 _ x + 1 by adding x + 1 in [1, x]’s formations. So now we can form all the numbers of [1, 2 _ x + 1]. After this iteration the new value of x becomes 2 * x + 1.


Similar Questions:

Title URL Difficulty
Coin Change https://leetcode.com/problems/coin-change Medium
Most Expensive Item That Can Not Be Bought https://leetcode.com/problems/most-expensive-item-that-can-not-be-bought Medium