Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1775 - Equal Sum Arrays With Minimum Number of Operations.

    Medium

    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 nums20 to 6. nums1 = 1,2,3,4,5,6, nums2 = **6** ,1,2,2,2,2.

    • Change nums15 to 1. nums1 = 1,2,3,4,5, **1** , nums2 = 6,1,2,2,2,2.

    • Change nums12 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 nums10 to 2. nums1 = **2** ,6, nums2 = 1.

    • Change nums11 to 2. nums1 = 2, **2** , nums2 = 1.

    • Change nums20 to 4. nums1 = 2,2, nums2 = **4** .

    Constraints:

    • <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>

    • 1 &lt;= nums1[i], nums2[i] &lt;= 6

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer minOperations(IntArray nums1, IntArray nums2)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait