Equal Sum Arrays With Minimum Number of Operations - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
Output: 3
💡 Note: Sum of nums1 is 21, sum of nums2 is 10. We need to reduce difference of 11. Change nums1[4] from 5→1 (gain 4), nums1[3] from 4→1 (gain 3), nums1[2] from 3→1 (gain 2). Total gain = 9. Change nums2[0] from 1→3 (gain 2). Total operations = 4, but we only need 3 to reduce difference to 0.
Example 2 — Equal Sums
$ Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6]
Output: 1
💡 Note: Sum of nums1 is 7, sum of nums2 is 6. Difference is 1. Change any element in nums1 from 1→0 is invalid, but change nums2[0] from 6→7 is invalid too. Actually, change nums1[0] from 1→2 reduces difference by -1, making sums equal. Wait, let me recalculate: change nums2[0] from 6→7 isn't valid since max is 6. Change nums1[6] from 1→0 isn't valid since min is 1. We can change nums2[0] from 6→5 (reduces sum by 1) or change nums1 element from 1→2 (increases sum by 1). Either takes 1 operation.
Example 3 — Impossible Case
$ Input: nums1 = [6,6], nums2 = [1]
Output: 3
💡 Note: nums1 sum is 12, nums2 sum is 1. Using greedy approach: swap to make nums1=[1], nums2=[6,6]. Need to reduce difference of 11. Available gains: increase nums1[0] from 1→6 (gain 5), decrease nums2[0] from 6→1 (gain 5), decrease nums2[1] from 6→1 (gain 5). After 3 operations with gains [5,5,1], the difference becomes 0.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 105
  • 1 ≤ nums1[i], nums2[i] ≤ 6

Visualization

Tap to expand
Equal Sum Arrays With Minimum Operations INPUT nums1 = [1,2,3,4,5,6] 1 2 3 4 5 6 Sum1 = 21 nums2 = [1,1,2,2,2,2] 1 1 2 2 2 2 Sum2 = 10 Difference = 11 (21 - 10 = 11) Goal: Make sums equal with minimum operations Values: 1 to 6 only ALGORITHM STEPS 1 Calculate Difference diff = |21 - 10| = 11 2 Find Max Gains nums1: 6--1=5, 5--1=4... nums2: 1--6=5, 1--6=5... 3 Sort by Impact Gains: [5,5,5,4,4,4,3,3,2,2,1,1] 4 Greedy Selection Pick largest gains first Operations: Op1: gain 5 (diff: 11-5=6) Op2: gain 5 (diff: 6-5=1) Op3: gain 5 (diff: 1-5=0) diff = 0, DONE! FINAL RESULT After 3 Operations: nums1 (unchanged): 1 2 3 4 5 6 Sum = 21 nums2 (modified): 6 6 2 5 2 0 Sum = 21 Output: 3 OK - Sums are Equal! 21 = 21 in 3 ops Key Insight: Greedy approach maximizes impact per operation. Each number in the smaller-sum array can gain up to (6 - value), and each number in the larger-sum array can reduce by (value - 1). Sort all possible gains and greedily pick the largest until the difference becomes zero. Time: O((m+n)log(m+n)) TutorialsPoint - Equal Sum Arrays With Minimum Number of Operations | Greedy - Maximize Impact
Asked in
Google 25 Amazon 18 Microsoft 15
32.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen