2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution

In this guide, you will get 2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Form Subsequence With Target Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Form Subsequence With Target Sum solution in C++
  4. Minimum Operations to Form Subsequence With Target Sum solution in Java
  5. Minimum Operations to Form Subsequence With Target Sum solution in Python
  6. Additional Resources
2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution image

Problem Statement of Minimum Operations to Form Subsequence With Target Sum

You are given a 0-indexed array nums consisting of non-negative powers of 2, and an integer target.
In one operation, you must apply the following changes to the array:

Choose any element of the array nums[i] such that nums[i] > 1.
Remove nums[i] from the array.
Add two occurrences of nums[i] / 2 to the end of nums.

Return the minimum number of operations you need to perform so that nums contains a subsequence whose elements sum to target. If it is impossible to obtain such a subsequence, return -1.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

Example 1:

Input: nums = [1,2,8], target = 7
Output: 1
Explanation: In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4].
At this stage, nums contains the subsequence [1,2,4] which sums up to 7.
It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7.

See also  2132. Stamping the Grid LeetCode Solution

Example 2:

Input: nums = [1,32,1,2], target = 12
Output: 2
Explanation: In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16].
In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8]
At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12.
It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12.
Example 3:

Input: nums = [1,32,1], target = 35
Output: -1
Explanation: It can be shown that no sequence of operations results in a subsequence that sums up to 35.

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 230
nums consists only of non-negative powers of two.
1 <= target < 231

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(31) = O(1)

2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution in C++

class Solution {
 public:
  int minOperations(vector<int>& nums, int target) {
    constexpr int kNoMissingBit = 31;
    constexpr int maxBit = 31;
    int ans = 0;
    int minMissingBit = kNoMissingBit;
    // count[i] := the number of occurrences of 2^i
    vector<int> count(maxBit + 1);

    for (const int num : nums)
      ++count[static_cast<int>(log2(num))];

    for (int bit = 0; bit < maxBit; ++bit) {
      // Check if `bit` is in the target.
      if (target >> bit & 1) {
        // If there are available bits, use one bit.
        if (count[bit] > 0)
          --count[bit];
        else
          minMissingBit = min(minMissingBit, bit);
      }
      // If we previously missed a bit and there are available bits.
      if (minMissingBit != kNoMissingBit && count[bit] > 0) {
        --count[bit];
        // Count the operations to break `bit` into `minMissingBit`.
        ans += bit - minMissingBit;
        minMissingBit = kNoMissingBit;
      }
      // Combining smaller numbers costs nothing.
      count[bit + 1] += count[bit] / 2;
    }

    // Check if all target bits have been covered, otherwise return -1.
    return minMissingBit == kNoMissingBit ? ans : -1;
  }
};
/* code provided by PROGIEZ */

2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution in Java

class Solution {
  public int minOperations(List<Integer> nums, int target) {
    final int kNoMissingBit = 31;
    final int maxBit = 31;
    int ans = 0;
    int minMissingBit = kNoMissingBit;
    // count[i] := the number of occurrences of 2^i
    int[] count = new int[maxBit + 1];

    for (final int num : nums)
      ++count[(int) (Math.log(num) / Math.log(2))];

    for (int bit = 0; bit < maxBit; ++bit) {
      // Check if `bit` is in the target.
      if ((target >> bit & 1) == 1) {
        // If there are available bits, use one bit.
        if (count[bit] > 0)
          --count[bit];
        else
          minMissingBit = Math.min(minMissingBit, bit);
      }
      // If we previously missed a bit and there are available bits.
      if (minMissingBit != kNoMissingBit && count[bit] > 0) {
        --count[bit];
        // Count the operations to break `bit` into `minMissingBit`.
        ans += bit - minMissingBit;
        minMissingBit = kNoMissingBit; // Set it to an the invalid value.
      }
      // Combining smaller numbers costs nothing.
      count[bit + 1] += count[bit] / 2;
    }

    // Check if all target bits have been covered, otherwise return -1.
    return minMissingBit == kNoMissingBit ? ans : -1;
  }
}
// code provided by PROGIEZ

2835. Minimum Operations to Form Subsequence With Target Sum LeetCode Solution in Python

class Solution:
  def minOperations(self, nums: list[int], target: int) -> int:
    kNoMissingBit = 31
    maxBit = 31
    ans = 0
    minMissingBit = kNoMissingBit
    # count[i] := the number of occurrences of 2^i
    count = collections.Counter(int(math.log2(num)) for num in nums)

    for bit in range(maxBit):
      # Check if `bit` is in the target.
      if target >> bit & 1:
        # If there are available bits, use one bit.
        if count[bit] > 0:
          count[bit] -= 1
        else:
          minMissingBit = min(minMissingBit, bit)
      # If we previously missed a bit and there are available bits.
      if minMissingBit != kNoMissingBit and count[bit] > 0:
        count[bit] -= 1
        # Count the operations to break `bit` into `minMissingBit`.
        ans += bit - minMissingBit
        minMissingBit = kNoMissingBit  # Set it to an the invalid value.
      # Combining smaller numbers costs nothing.
      count[bit + 1] += count[bit] // 2

    # Check if all target bits have been covered, otherwise return -1.
    return ans if minMissingBit == maxBit else -1
# code by PROGIEZ

Additional Resources

See also  1263. Minimum Moves to Move a Box to Their Target Location LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.