Equal Sum Arrays With Minimum Number of Operations

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
Inputnums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
Output3
You can make the sums of nums1 and nums2 equal with 3 operations by increasing one value in nums2 and decreasing two values in nums1.
Example 2
Inputnums1 = [1,1,1,1,1,1,1], nums2 = [6]
Output-1
There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.

Constraints

  • 1 <= nums1.length, nums2.length <= 10^5
  • 1 <= nums1[i], nums2[i] <= 6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate