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 integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1:
i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] - k.nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < n, nums1[i] == nums2[i].
Return the minimum number of operations required to make nums1 equal to nums2. If it is impossible to make them equal, return -1.
Example 1:
Input: nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3 Output: 2 Explanation: In 2 operations, we can transform nums1 to nums2. 1st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4]. 2nd operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1]. One can prove that it is impossible to make arrays equal in fewer operations.
Example 2:
Input: nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1 Output: -1 Explanation: It can be proved that it is impossible to make the two arrays equal.
Constraints:
n == nums1.length == nums2.length2 <= n <= 1050 <= nums1[i], nums2[j] <= 1090 <= k <= 105Problem summary: You are given two integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1: Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] - k. nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < n, nums1[i] == nums2[i]. Return the minimum number of operations required to make nums1 equal to nums2. If it is impossible to make them equal, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[4,3,1,4] [1,3,7,1] 3
[3,8,5,2] [2,4,1,6] 1
minimum-operations-to-make-array-equal)minimum-number-of-operations-to-make-arrays-similar)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2541: Minimum Operations to Make Array Equal II
class Solution {
public long minOperations(int[] nums1, int[] nums2, int k) {
long ans = 0, x = 0;
for (int i = 0; i < nums1.length; ++i) {
int a = nums1[i], b = nums2[i];
if (k == 0) {
if (a != b) {
return -1;
}
continue;
}
if ((a - b) % k != 0) {
return -1;
}
int y = (a - b) / k;
ans += Math.abs(y);
x += y;
}
return x == 0 ? ans / 2 : -1;
}
}
// Accepted solution for LeetCode #2541: Minimum Operations to Make Array Equal II
func minOperations(nums1 []int, nums2 []int, k int) int64 {
ans, x := 0, 0
for i, a := range nums1 {
b := nums2[i]
if k == 0 {
if a != b {
return -1
}
continue
}
if (a-b)%k != 0 {
return -1
}
y := (a - b) / k
ans += abs(y)
x += y
}
if x != 0 {
return -1
}
return int64(ans / 2)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2541: Minimum Operations to Make Array Equal II
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int], k: int) -> int:
ans = x = 0
for a, b in zip(nums1, nums2):
if k == 0:
if a != b:
return -1
continue
if (a - b) % k:
return -1
y = (a - b) // k
ans += abs(y)
x += y
return -1 if x else ans // 2
// Accepted solution for LeetCode #2541: Minimum Operations to Make Array Equal II
impl Solution {
pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
let k = k as i64;
let n = nums1.len();
if k == 0 {
return if nums1.iter().enumerate().all(|(i, &v)| v == nums2[i]) {
0
} else {
-1
};
}
let mut sum1 = 0;
let mut sum2 = 0;
for i in 0..n {
let diff = (nums1[i] - nums2[i]) as i64;
sum1 += diff;
if diff % k != 0 {
return -1;
}
sum2 += diff.abs();
}
if sum1 != 0 {
return -1;
}
sum2 / (k * 2)
}
}
// Accepted solution for LeetCode #2541: Minimum Operations to Make Array Equal II
function minOperations(nums1: number[], nums2: number[], k: number): number {
const n = nums1.length;
if (k === 0) {
return nums1.every((v, i) => v === nums2[i]) ? 0 : -1;
}
let sum1 = 0;
let sum2 = 0;
for (let i = 0; i < n; i++) {
const diff = nums1[i] - nums2[i];
sum1 += diff;
if (diff % k !== 0) {
return -1;
}
sum2 += Math.abs(diff);
}
if (sum1 !== 0) {
return -1;
}
return sum2 / (k * 2);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
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.