<p>You are given a <strong>0-indexed</strong> integer array <code>coins</code>, representing the values of the coins available, and an integer <code>target</code>.</p>

<p>An integer <code>x</code> is <strong>obtainable</strong> if there exists a subsequence of <code>coins</code> that sums to <code>x</code>.</p>

<p>Return <em>the<strong> minimum</strong> number of coins <strong>of any value</strong> that need to be added to the array so that every integer in the range</em> <code>[1, target]</code><em> is <strong>obtainable</strong></em>.</p>

<p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> coins = [1,4,10], target = 19
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> coins = [1,4,10,5,7,19], target = 19
<strong>Output:</strong> 1
<strong>Explanation:</strong> 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. 
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> coins = [1,1,1], target = 20
<strong>Output:</strong> 3
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= coins.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= coins[i] &lt;= target</code></li>
</ul>

# Minimum Number of Coins to be Added

from typing import List

class Solution:
  def minimumAddedCoins(self, coins: List[int], target: int) -> int:
    # Sort the coins to process them in increasing order
    coins.sort()

    max_reachable = 0  # The maximum value we can reach with the current set of coins
    added_coins = 0  # Number of coins we have added
    index = 0  # Index for iterating through the sorted list of coins

    # While we have not reached or exceeded the target value
    while max_reachable < target:
      if index < len(coins) and coins[index] <= max_reachable + 1:
        # The current coin can be used to extend the range of reachable sums
        max_reachable += coins[index]  # Update the maximum reachable value
        index += 1  # Move to the next coin
      else:
        # We need to add a new coin to cover the gap
        new_coin = max_reachable + 1  # This is the smallest coin we need to add
        max_reachable += new_coin  # Update the maximum reachable value with the new coin
        added_coins += 1  # Increment the count of added coins

    return added_coins  # Return the total number of coins added

# Time Complexity: O(nlogn)
# Space Complexity: O(1)
194