The Minimum Sum of Squared Difference problem gives you two integer arrays of equal length and a budget of k total operations. In each operation, you can decrease any element of the first array by 1. After using at most k operations, compute the minimum possible sum of squared differences: sum((arr1[i] - arr2[i])^2). This Minimum Sum of Squared Difference coding problem requires greedy budget allocation using a max-heap.
Uber, Microsoft, and Amazon ask this to test heap-based greedy strategies under a budget constraint. The key insight — that you should always reduce the largest current difference first, since squaring makes large values disproportionately costly — demonstrates understanding of the convexity of the squared function. The array, sorting, binary search, heap, and greedy interview pattern is central.
Use a max-heap based on absolute differences |arr1[i] - arr2[i]|. In each operation, extract the largest difference, reduce it by 1 (or reduce it by as much as possible within the remaining budget), and push back the updated difference. To optimize, instead of reducing one step at a time, compute how many steps it takes to bring the top element equal to the second-largest. Perform that many reductions at once, then distribute remaining budget proportionally across elements at the same level.
arr1 = [1, 4, 10], arr2 = [2, 4, 7], k = 3. Differences: |1-2|=1, |4-4|=0, |10-7|=3.
When minimizing a sum of squared (or convex) values under a budget of reductions, always reduce the largest value first — this is the greedy choice that minimizes total squared sum due to convexity. The heap provides efficient access to the current maximum. Practice bulk-reduction optimization to avoid TLE on large k values — the step of "reduce all top-k equal maximums simultaneously" is the key efficiency improvement.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sell Diminishing-Valued Colored Balls | Medium | Solve | |
| Maximum Subsequence Score | Medium | Solve | |
| Maximize Score of Numbers in Ranges | Medium | Solve | |
| Maximum Number of Events That Can Be Attended | Medium | Solve | |
| Maximum Tastiness of Candy Basket | Medium | Solve |