2541. Minimum Operations to Make Array Equal II LeetCode Solution
In this guide, you will get 2541. Minimum Operations to Make Array Equal II LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make Array Equal II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Operations to Make Array Equal II solution in C++
- Minimum Operations to Make Array Equal II solution in Java
- Minimum Operations to Make Array Equal II solution in Python
- Additional Resources

Problem Statement of Minimum Operations to Make Array Equal II
You are given two integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1:
Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] – k.
nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < n, nums1[i] == nums2[i].
Return the minimum number of operations required to make nums1 equal to nums2. 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.
1st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
2nd 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
2 <= n <= 105
0 <= nums1[i], nums2[j] <= 109
0 <= k <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2541. Minimum Operations to Make Array Equal II LeetCode Solution in C++
class Solution {
public:
long long minOperations(vector<int>& nums1, vector<int>& nums2, int k) {
if (k == 0)
return nums1 == nums2 ? 0 : -1;
long ans = 0;
// the number of increments - the number of decrements
long opsDiff = 0;
for (int i = 0; i < nums1.size(); ++i) {
const int diff = nums1[i] - nums2[i];
if (diff == 0)
continue;
if (diff % k != 0)
return -1;
const int ops = diff / k;
opsDiff += ops;
ans += abs(ops);
}
return opsDiff == 0 ? ans / 2 : -1;
}
};
/* code provided by PROGIEZ */
2541. Minimum Operations to Make Array Equal II LeetCode Solution in Java
class Solution {
public long minOperations(int[] nums1, int[] nums2, int k) {
if (k == 0)
return Arrays.equals(nums1, nums2) ? 0 : -1;
long ans = 0;
long opsDiff = 0; // the number of increments - the number of decrements
for (int i = 0; i < nums1.length; ++i) {
final int diff = nums1[i] - nums2[i];
if (diff == 0)
continue;
if (diff % k != 0)
return -1;
final int ops = diff / k;
opsDiff += ops;
ans += Math.abs(ops);
}
return opsDiff == 0 ? ans / 2 : -1;
}
}
// code provided by PROGIEZ
2541. Minimum Operations to Make Array Equal II LeetCode Solution in Python
class Solution:
def minOperations(self, nums1: list[int], nums2: list[int], k: int) -> int:
if k == 0:
return 0 if nums1 == nums2 else -1
ans = 0
opsDiff = 0 # the number of increments - number of decrements
for num1, num2 in zip(nums1, nums2):
diff = num1 - num2
if diff == 0:
continue
if diff % k != 0:
return -1
ops = diff // k
opsDiff += ops
ans += abs(ops)
return ans // 2 if opsDiff == 0 else -1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.