---
# System prepended metadata

title: 2541. Minimum Operations to Make Array Equal II
tags: [Leetcode, Medium, Math, Greedy]

---

# 2541. Minimum Operations to Make Array Equal II
###### tags: `Leetcode` `Medium` `Greedy` `Math`
Link: https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/description/
## 思路
我们先把每一次的+k -k都当作一个operation
+k的score是1 -k的score是-1
那么如果最后score积累起来是0
就说明可以make array equal
再用ans记录operation数 除以2就是答案
## Code
```python=
class Solution:
    def minOperations(self, nums1: List[int], nums2: List[int], k: int) -> int:
        curr = 0
        ans = 0
        for a, b in zip(nums1, nums2):
            if k==0:
                if b!=a:
                    return -1
            else:
                if (b-a)%k!=0:
                    return -1
                curr += (b-a)//k
                ans += abs((b-a)//k)
        if curr!=0: 
            return -1
        else:
            return ans//2
```