Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])2 for each 0 <= i < n.
You are also given two positive integers k1 and k2. You can modify any of the elements of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements of nums2 by +1 or -1 at most k2 times.
Return the minimum sum of squared difference after modifying array nums1 at most k1 times and modifying array nums2 at most k2 times.
Note: You are allowed to modify the array elements to become negative integers.
Example 1:
Input: nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0 Output: 579 Explanation: The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0. The sum of square difference will be: (1 - 2)2 + (2 - 10)2 + (3 - 20)2 + (4 - 19)2 = 579.
Example 2:
Input: nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1 Output: 43 Explanation: One way to obtain the minimum sum of square difference is: - Increase nums1[0] once. - Increase nums2[2] once. The minimum of the sum of square difference will be: (2 - 5)2 + (4 - 8)2 + (10 - 7)2 + (12 - 9)2 = 43. Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1050 <= nums1[i], nums2[i] <= 1050 <= k1, k2 <= 109Problem summary: You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n. The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])2 for each 0 <= i < n. You are also given two positive integers k1 and k2. You can modify any of the elements of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements of nums2 by +1 or -1 at most k2 times. Return the minimum sum of squared difference after modifying array nums1 at most k1 times and modifying array nums2 at most k2 times. Note: You are allowed to modify the array elements to become negative integers.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy
[1,2,3,4] [2,10,20,19] 0 0
[1,4,10,12] [5,8,6,9] 1 1
minimum-absolute-sum-difference)partition-array-into-two-arrays-to-minimize-sum-difference)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
class Solution {
public long minSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {
int n = nums1.length;
int[] d = new int[n];
long s = 0;
int mx = 0;
int k = k1 + k2;
for (int i = 0; i < n; ++i) {
d[i] = Math.abs(nums1[i] - nums2[i]);
s += d[i];
mx = Math.max(mx, d[i]);
}
if (s <= k) {
return 0;
}
int left = 0, right = mx;
while (left < right) {
int mid = (left + right) >> 1;
long t = 0;
for (int v : d) {
t += Math.max(v - mid, 0);
}
if (t <= k) {
right = mid;
} else {
left = mid + 1;
}
}
for (int i = 0; i < n; ++i) {
k -= Math.max(0, d[i] - left);
d[i] = Math.min(d[i], left);
}
for (int i = 0; i < n && k > 0; ++i) {
if (d[i] == left) {
--k;
--d[i];
}
}
long ans = 0;
for (int v : d) {
ans += (long) v * v;
}
return ans;
}
}
// Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
func minSumSquareDiff(nums1 []int, nums2 []int, k1 int, k2 int) int64 {
k := k1 + k2
s, mx := 0, 0
n := len(nums1)
d := make([]int, n)
for i, v := range nums1 {
d[i] = abs(v - nums2[i])
s += d[i]
mx = max(mx, d[i])
}
if s <= k {
return 0
}
left, right := 0, mx
for left < right {
mid := (left + right) >> 1
t := 0
for _, v := range d {
t += max(v-mid, 0)
}
if t <= k {
right = mid
} else {
left = mid + 1
}
}
for i, v := range d {
k -= max(v-left, 0)
d[i] = min(v, left)
}
for i, v := range d {
if k <= 0 {
break
}
if v == left {
d[i]--
k--
}
}
ans := 0
for _, v := range d {
ans += v * v
}
return int64(ans)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
class Solution:
def minSumSquareDiff(
self, nums1: List[int], nums2: List[int], k1: int, k2: int
) -> int:
d = [abs(a - b) for a, b in zip(nums1, nums2)]
k = k1 + k2
if sum(d) <= k:
return 0
left, right = 0, max(d)
while left < right:
mid = (left + right) >> 1
if sum(max(v - mid, 0) for v in d) <= k:
right = mid
else:
left = mid + 1
for i, v in enumerate(d):
d[i] = min(left, v)
k -= max(0, v - left)
for i, v in enumerate(d):
if k == 0:
break
if v == left:
k -= 1
d[i] -= 1
return sum(v * v for v in d)
// Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
/**
* [2333] Minimum Sum of Squared Difference
*
* You are given two positive 0-indexed integer arrays nums1 and nums2, both of length n.
* The sum of squared difference of arrays nums1 and nums2 is defined as the sum of (nums1[i] - nums2[i])^2 for each 0 <= i < n.
* You are also given two positive integers k1 and k2. You can modify any of the elements of nums1 by +1 or -1 at most k1 times. Similarly, you can modify any of the elements of nums2 by +1 or -1 at most k2 times.
* Return the minimum sum of squared difference after modifying array nums1 at most k1 times and modifying array nums2 at most k2 times.
* Note: You are allowed to modify the array elements to become negative integers.
*
* Example 1:
*
* Input: nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
* Output: 579
* Explanation: The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0.
* The sum of square difference will be: (1 - 2)^2 + (2 - 10)^2 + (3 - 20)^2 + (4 - 19)^2 = 579.
*
* Example 2:
*
* Input: nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
* Output: 43
* Explanation: One way to obtain the minimum sum of square difference is:
* - Increase nums1[0] once.
* - Increase nums2[2] once.
* The minimum of the sum of square difference will be:
* (2 - 5)^2 + (4 - 8)^2 + (10 - 7)^2 + (12 - 9)^2 = 43.
* Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.
*
* Constraints:
*
* n == nums1.length == nums2.length
* 1 <= n <= 10^5
* 0 <= nums1[i], nums2[i] <= 10^5
* 0 <= k1, k2 <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-sum-of-squared-difference/
// discuss: https://leetcode.com/problems/minimum-sum-of-squared-difference/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_sum_square_diff(nums1: Vec<i32>, nums2: Vec<i32>, k1: i32, k2: i32) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2333_example_1() {
let nums1 = vec![1, 2, 3, 4];
let nums2 = vec![2, 10, 20, 19];
let k1 = 0;
let k2 = 0;
let result = 579;
assert_eq!(Solution::min_sum_square_diff(nums1, nums2, k1, k2), result);
}
#[test]
#[ignore]
fn test_2333_example_2() {
let nums1 = vec![1, 4, 10, 12];
let nums2 = vec![5, 8, 6, 9];
let k1 = 1;
let k2 = 1;
let result = 43;
assert_eq!(Solution::min_sum_square_diff(nums1, nums2, k1, k2), result);
}
}
// Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2333: Minimum Sum of Squared Difference
// class Solution {
// public long minSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {
// int n = nums1.length;
// int[] d = new int[n];
// long s = 0;
// int mx = 0;
// int k = k1 + k2;
// for (int i = 0; i < n; ++i) {
// d[i] = Math.abs(nums1[i] - nums2[i]);
// s += d[i];
// mx = Math.max(mx, d[i]);
// }
// if (s <= k) {
// return 0;
// }
// int left = 0, right = mx;
// while (left < right) {
// int mid = (left + right) >> 1;
// long t = 0;
// for (int v : d) {
// t += Math.max(v - mid, 0);
// }
// if (t <= k) {
// right = mid;
// } else {
// left = mid + 1;
// }
// }
// for (int i = 0; i < n; ++i) {
// k -= Math.max(0, d[i] - left);
// d[i] = Math.min(d[i], left);
// }
// for (int i = 0; i < n && k > 0; ++i) {
// if (d[i] == left) {
// --k;
// --d[i];
// }
// }
// long ans = 0;
// for (int v : d) {
// ans += (long) v * v;
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.