Class Solution
-
- All Implemented Interfaces:
public final class Solution2541 - Minimum Operations to Make Array Equal II\.
Medium
You are given two integer arrays
nums1andnums2of equal lengthnand an integerk. You can perform the following operation onnums1:Choose two indexes
iandjand incrementnums1[i]bykand decrementnums1[j]byk. In other words,nums1[i] = nums1[i] + kandnums1[j] = nums1[j] - k.
nums1is said to be equal tonums2if for all indicesisuch that0 <= i < n,nums1[i] == nums2[i].Return the minimum number of operations required to make
nums1equal tonums2. 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.
1<sup>st</sup> operation: i = 2, j = 0. After applying the operation, nums1 = 1,3,4,4.
2<sup>nd</sup> 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.length<code>2 <= n <= 10<sup>5</sup></code>
<code>0 <= nums1i, nums2j<= 10<sup>9</sup></code>
<code>0 <= k <= 10<sup>5</sup></code>