[Hacker Earth] Count PermutationsRead more
[Hacker Earth] Two Dimensional ORRead more
[Codeforces] Round 264 (Div. 2) C. Gargari and BishopsRead more
[Codeforces] Round 264 (Div. 2) D. Gargari and PermutationsRead more
[Codeforces] Round 270 D. Design Tutorial: Inverse the ProblemRead more
[Codeforces] Round 272 (Div. 2) D. Dreamoon and SetsRead more
[Codeforces] Round 274 (Div. 2) E. Riding in a LiftRead more
[Codeforces] Round 278 (Div. 2) B. Candy BoxesRead more
[LeetCode] Maximize Value of Function in a Ball Passing GameRead more
[LeetCode] Minimum Operations to Form Subsequence With Target Sum

2835. 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.

Read more