Skip to main content

Minimum Operations to Form Subset Sum II - Video Solutions

Hard

Super Hard💀Math + DP DSA Question asked by Leetcode in Weekly Contest 517(Q4,4041)

Kumar K [Amazon]
40:40682 views
7 video solutions available

Minimum Operations to Form Subset Sum II - Video Solution

Watch 7 video solutions for Minimum Operations to Form Subset Sum II, a hard level problem. This walkthrough by Kumar K [Amazon] has 682 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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] = 10 once: 10 → 5, costing 1 operation.
  • Multiply nums[1] = 2 twice: 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] = 3 into 2 using 2 operations:
    • Divide nums[1] to get 1.
    • Multiply nums[1] = 1 to get 2.
  • 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 nums sum to 7, so the answer is -1.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 500
  • 1 <= sum <= 5000
Read full problem with examples

Approach Overview

Problem Overview: You're given an array and a target sum. You need the minimum number of operations (insertions, deletions, or modifications depending on the exact statement) to make some subset of the array sum to the target. This is a variation of the classic subset sum problem, but instead of just checking feasibility, you're optimizing the cost to reach the target.

Approach 1: Brute Force - Enumerate All Subsets (O(2^n) time, O(n) space)

Generate every subset using recursion or bitmasks, compute the sum, and track the minimum operations needed to reach the target. This is only viable for n ≤ 20. It shows you understand the subset-sum structure, but it won't scale. Use it only as a sanity check for small test cases.

Approach 2: 0-1 Knapsack DP - Bottom-Up (O(n * target) time, O(target) space)

This is the optimal approach. Define dp[s] as the minimum operations needed to form sum s using the items processed so far. Initialize dp[0] = 0 and all other states to infinity. For each number num in the array, iterate s from target down to num and update dp[s] = min(dp[s], dp[s - num] + cost). The reverse iteration is critical — it ensures each number is used at most once, which is the defining property of 0-1 Knapsack. The answer is dp[target] if it's finite, otherwise -1.

Approach 3: 0-1 Knapsack DP - Top-Down with Memoization (O(n * target) time, O(n * target) space)

Use recursion with a memo table memo[i][s] representing the minimum operations to reach sum s using the first i items. At each step, either skip the current item or take it if it doesn't exceed the target. This approach is more intuitive for some people, but the space overhead is higher. Prefer it when you need to reconstruct the exact subset or when the recursive formulation is easier to reason about.

Recommended for interviews: Interviewers expect the bottom-up 0-1 Knapsack DP. The brute force shows you understand the problem, but the optimized DP demonstrates that you can recognize the classic dynamic programming pattern and apply it under constraints. Always start by stating the DP state and transition, then implement the space-optimized version. This problem is a direct application of the knapsack pattern, so mastering the 0-1 Knapsack template is the key to solving it quickly.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force - Enumerate All SubsetsO(2^n)O(n)Only for n ≤ 20 or as a brute-force validator
0-1 Knapsack DP - Bottom-UpO(n * target)O(target)General case; optimal for most constraints
0-1 Knapsack DP - Top-Down MemoizationO(n * target)O(n * target)When you need to reconstruct the subset or prefer recursion