Minimum Operations to Form Subsequence With Target Sum
You're given an array nums containing only powers of 2 (like 1, 2, 4, 8, 16...) and need to find the minimum number of operations to create a subsequence that sums to a target value.
The Operation: You can split any element nums[i] (where nums[i] > 1) into two smaller pieces of nums[i] / 2 each. For example, splitting 8 gives you two 4's.
Goal: Return the minimum operations needed so that you can select some elements from the array (a subsequence) that sum exactly to target. If impossible, return -1.
Example: If nums = [1, 2, 8] and target = 7, you could split 8 into two 4's, then split one 4 into two 2's, giving you [1, 2, 4, 2, 2]. Now you can pick [1, 2, 4] to sum to 7.
Input & Output
Constraints
- 1 โค nums.length โค 1000
- 1 โค nums[i] โค 230
- nums[i] is a power of 2
- 1 โค target โค 231 - 1