4041. Minimum Operations to Form Subset Sum II
Description
You are given an integer array nums and an integer sum.
In one operation, choose an element with current value x and replace it with either 2 * x or floor(x / 2).
For each element, multiplication and division operations may be performed in any order.
Return the minimum number of operations needed so that some subset of the resulting array has a sum exactly equal to sum. If it is impossible, return -1.
The floor() function returns the integer part of the division.
Example 1:
Input: nums = [10,2], sum = 13
Output: 3
Explanation:
- Divide
nums[0] = 10once:10 → 5, costing 1 operation. - Multiply
nums[1] = 2twice:2 → 4 → 8, costing 2 operations. - After these operations,
nums = [5, 8]. The subset{5, 8}sums to 13 using 3 operations in total.
Example 2:
Input: nums = [6,3], sum = 8
Output: 2
Explanation:
- Turn
nums[1] = 3into 2 using 2 operations:- Divide
nums[1]to get 1. - Multiply
nums[1] = 1to get 2.
- Divide
- After these operations,
nums = [6, 2]. The subset{6, 2}sums to 8 using 2 operations in total.
Example 3:
Input: nums = [2,2], sum = 7
Output: -1
Explanation:
- No sequence of operations lets a subset of
numssum to 7, so the answer is -1.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 5001 <= sum <= 5000
Solutions
Solution 1: 0-1 Knapsack
Thinking
Unlike the previous problem, multiplies and divides may be interleaved. A multiply that happens before a divide cancels, so every sequence reduces to \(i\) divides followed by \(j\) multiplies: the value is \(\lfloor x/2^i\rfloor\times 2^j\) at cost \(i+j\).
We still fill capacity \(\textit{sum}\) with a \(0\)-\(1\) knapsack, now enumerating an extra dimension per element. Backward updates keep at most one pair \((i,j)\) per element.
If \(f[\textit{sum}]\) stays infinite the answer is \(-1\).
Unlike the previous problem, multiplications and divisions may be interleaved in any order. Notice that a multiplication immediately followed by a division is a no-op, since \(\lfloor 2x / 2 \rfloor = x\), so any multiplication that happens before a division can be cancelled against it, wasting two operations. After repeatedly cancelling such pairs, every sequence reduces to "divide \(i\) times, then multiply \(j\) times", which turns \(x\) into \(\lfloor x / 2^i \rfloor \times 2^j\) at a cost of \(i + j\) operations.
This turns the problem into a 0-1 knapsack: every element contributes at most one (value, cost) pair, and we want the minimum cost to fill a capacity of exactly \(\textit{sum}\).
We define \(f[w]\) as the minimum number of operations needed for a subset to sum to exactly \(w\), with \(f[0] = 0\) and all other entries set to \(+\infty\). For each element \(x\), we iterate the capacity \(w\) from large to small, enumerate the number of divisions \(i\) and multiplications \(j\) to get the value \(y = \lfloor x / 2^i \rfloor \times 2^j\), and update \(f[w]\) with \(f[w - y] + i + j\) whenever \(y \leq w\). If \(f[\textit{sum}]\) is still \(+\infty\) at the end, no valid sequence of operations exists and we return \(-1\); otherwise we return \(f[\textit{sum}]\).
The time complexity is \(O(n \times S \times \log M \times \log S)\), and the space complexity is \(O(S)\). Here, \(n\) and \(M\) are the length and the maximum value of the array \(\textit{nums}\), and \(S\) is the given \(\textit{sum}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |