AlgoInsight

DSA problem

Last Stone Weight II

Medium0/1 KnapsackPattern: 0/1 Knapsacktop300AmazonGoogleMicrosoftBloombergAdobe

You are given an array of integers stones where stones[i] is the weight of the i-th stone. We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of the smash is: if x == y, both stones are destroyed; otherwise, the stone of weight x is destroyed and the stone of weight y has new weight y - x. At the end of the game, there is at most one stone left. Return the smallest possible weight of the left stone, or 0 if there are no stones left.

The greedy heap simulation (always smashing the two heaviest stones together) solves the basic "Last Stone Weight" variant but fails here because we are now allowed to combine stones in any order, and order matters for the final weight. The key insight is that each smash sequence corresponds to assigning each stone a sign of + or - — the final weight equals the absolute value of the signed sum.

So the problem reduces to partitioning stones into two disjoint groups A and B such that |sum(A) - sum(B)| is minimized. Equivalently, we want to choose a subset A with sum(A) as close to sum(stones) / 2 as possible, since then the other half is sum - sum(A) and the gap is sum - 2 * sum(A).

This is a classic 0/1 knapsack reachability problem. Build a boolean DP over capacities 0 to sum / 2, where dp[s] is true if some subset sums to exactly s. The largest reachable s <= sum / 2 gives the best partition, and the answer is sum - 2 * s. The full algorithm is O(n * sum) time and O(sum) space, well within the constraints.

Examples

Example 1
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation: One optimal partition is {2,4,8} vs {7,1,1}, summing to 14 and 9. The absolute difference is 5 — but a better partition {2,7,1} vs {4,1,8} gives 10 vs 13, a difference of 3. The true optimum here yields 1 via partition {1,4,7,1} vs {2,8} = 13 vs 10? Better still: {2,4,7,1,1} vs {8} = 15 vs 8 → 7. The smallest reachable difference for this set is 1.
Example 2
Input: stones = [31,26,33,21,40]
Output: 5
Explanation: The total sum is 151 (odd). The closest reachable subset sum to 75 (floor(151/2)) is 73, giving an absolute difference of 151 - 2*73 = 5.
Example 3
Input: stones = [1,2]
Output: 1
Explanation: Total sum is 3, closest reachable subset sum to 1 is 1, giving difference 3 - 2*1 = 1.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

dynamic-programming0-1-knapsacksubset-sumarray

Browse all DSA problems