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 arrays of integers nums1 and nums2, possibly of different lengths. The values in the arrays are between 1 and 6, inclusive.
In one operation, you can change any integer's value in any of the arrays to any value between 1 and 6, inclusive.
Return the minimum number of operations required to make the sum of values in nums1 equal to the sum of values in nums2. Return -1 if it is not possible to make the sum of the two arrays equal.
Example 1:
Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2] Output: 3 Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2]. - Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2]. - Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2].
Example 2:
Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6] Output: -1 Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
Example 3:
Input: nums1 = [6,6], nums2 = [1] Output: 3 Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. - Change nums1[0] to 2. nums1 = [2,6], nums2 = [1]. - Change nums1[1] to 2. nums1 = [2,2], nums2 = [1]. - Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].
Constraints:
1 <= nums1.length, nums2.length <= 1051 <= nums1[i], nums2[i] <= 6Problem summary: You are given two arrays of integers nums1 and nums2, possibly of different lengths. The values in the arrays are between 1 and 6, inclusive. In one operation, you can change any integer's value in any of the arrays to any value between 1 and 6, inclusive. Return the minimum number of operations required to make the sum of values in nums1 equal to the sum of values in nums2. Return -1 if it is not possible to make the sum of the two arrays equal.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,2,3,4,5,6] [1,1,2,2,2,2]
[1,1,1,1,1,1,1] [6]
[6,6] [1]
number-of-dice-rolls-with-target-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
class Solution {
public int minOperations(int[] nums1, int[] nums2) {
int s1 = Arrays.stream(nums1).sum();
int s2 = Arrays.stream(nums2).sum();
if (s1 == s2) {
return 0;
}
if (s1 > s2) {
return minOperations(nums2, nums1);
}
int d = s2 - s1;
int[] arr = new int[nums1.length + nums2.length];
int k = 0;
for (int v : nums1) {
arr[k++] = 6 - v;
}
for (int v : nums2) {
arr[k++] = v - 1;
}
Arrays.sort(arr);
for (int i = 1, j = arr.length - 1; j >= 0; ++i, --j) {
d -= arr[j];
if (d <= 0) {
return i;
}
}
return -1;
}
}
// Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
func minOperations(nums1 []int, nums2 []int) int {
s1, s2 := sum(nums1), sum(nums2)
if s1 == s2 {
return 0
}
if s1 > s2 {
return minOperations(nums2, nums1)
}
d := s2 - s1
arr := []int{}
for _, v := range nums1 {
arr = append(arr, 6-v)
}
for _, v := range nums2 {
arr = append(arr, v-1)
}
sort.Sort(sort.Reverse(sort.IntSlice(arr)))
for i, v := range arr {
d -= v
if d <= 0 {
return i + 1
}
}
return -1
}
func sum(nums []int) (s int) {
for _, v := range nums {
s += v
}
return
}
# Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
s1, s2 = sum(nums1), sum(nums2)
if s1 == s2:
return 0
if s1 > s2:
return self.minOperations(nums2, nums1)
arr = [6 - v for v in nums1] + [v - 1 for v in nums2]
d = s2 - s1
for i, v in enumerate(sorted(arr, reverse=True), 1):
d -= v
if d <= 0:
return i
return -1
// Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
/**
* [1775] Equal Sum Arrays With Minimum Number of Operations
*
* You are given two arrays of integers nums1 and <font face="monospace">nums2</font>, possibly of different lengths. The values in the arrays are between 1 and 6, inclusive.
* In one operation, you can change any integer's value in any of the arrays to any value between 1 and 6, inclusive.
* Return the minimum number of operations required to make the sum of values in nums1 equal to the sum of values in nums2. Return -1 if it is not possible to make the sum of the two arrays equal.
*
* Example 1:
*
* Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
* Output: 3
* Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
* - Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [<u>6</u>,1,2,2,2,2].
* - Change nums1[5] to 1. nums1 = [1,2,3,4,5,<u>1</u>], nums2 = [6,1,2,2,2,2].
* - Change nums1[2] to 2. nums1 = [1,2,<u>2</u>,4,5,1], nums2 = [6,1,2,2,2,2].
*
* Example 2:
*
* Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6]
* Output: -1
* Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
*
* Example 3:
*
* Input: nums1 = [6,6], nums2 = [1]
* Output: 3
* Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
* - Change nums1[0] to 2. nums1 = [<u>2</u>,6], nums2 = [1].
* - Change nums1[1] to 2. nums1 = [2,<u>2</u>], nums2 = [1].
* - Change nums2[0] to 4. nums1 = [2,2], nums2 = [<u>4</u>].
*
*
* Constraints:
*
* 1 <= nums1.length, nums2.length <= 10^5
* 1 <= nums1[i], nums2[i] <= 6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/
// discuss: https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let a: i32 = nums1.iter().map(|&x| x as i32).sum();
let b = nums2.iter().map(|&x| x as i32).sum();
if (a < b) {
return Self::min_operations(nums2, nums1);
}
let mut pq = std::collections::BinaryHeap::new();
let mut amount = a - b;
for n in nums1 {
if n > 1 {
pq.push(n - 1);
}
}
for n in nums2 {
if n < 6 {
pq.push(6 - n);
}
}
let mut result = 0;
while amount > 0 && pq.is_empty() == false {
amount -= pq.pop().unwrap();
result += 1;
}
if amount > 0 {
return -1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1775_example_1() {
let nums1 = vec![1, 2, 3, 4, 5, 6];
let nums2 = vec![1, 1, 2, 2, 2, 2];
let result = 3;
assert_eq!(Solution::min_operations(nums1, nums2), result);
}
#[test]
fn test_1775_example_2() {
let nums1 = vec![1, 1, 1, 1, 1, 1, 1];
let nums2 = vec![6];
let result = -1;
assert_eq!(Solution::min_operations(nums1, nums2), result);
}
#[test]
fn test_1775_example_3() {
let nums1 = vec![6, 6];
let nums2 = vec![1];
let result = 3;
assert_eq!(Solution::min_operations(nums1, nums2), result);
}
}
// Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1775: Equal Sum Arrays With Minimum Number of Operations
// class Solution {
// public int minOperations(int[] nums1, int[] nums2) {
// int s1 = Arrays.stream(nums1).sum();
// int s2 = Arrays.stream(nums2).sum();
// if (s1 == s2) {
// return 0;
// }
// if (s1 > s2) {
// return minOperations(nums2, nums1);
// }
// int d = s2 - s1;
// int[] arr = new int[nums1.length + nums2.length];
// int k = 0;
// for (int v : nums1) {
// arr[k++] = 6 - v;
// }
// for (int v : nums2) {
// arr[k++] = v - 1;
// }
// Arrays.sort(arr);
// for (int i = 1, j = arr.length - 1; j >= 0; ++i, --j) {
// d -= arr[j];
// if (d <= 0) {
// return i;
// }
// }
// return -1;
// }
// }
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
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.