1775. Equal Sum Arrays With Minimum Number of Operations LeetCode Solution
In this guide, you will get 1775. Equal Sum Arrays With Minimum Number of Operations LeetCode Solution with the best time and space complexity. The solution to Equal Sum Arrays With Minimum Number of Operations 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
- Equal Sum Arrays With Minimum Number of Operations solution in C++
- Equal Sum Arrays With Minimum Number of Operations solution in Java
- Equal Sum Arrays With Minimum Number of Operations solution in Python
- Additional Resources

Problem Statement of 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:
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 nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2].
– Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2].
– Change nums1[2] 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 nums1[0] to 2. nums1 = [2,6], nums2 = [1].
– Change nums1[1] to 2. nums1 = [2,2], nums2 = [1].
– Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].
Constraints:
1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[i] <= 6
Complexity Analysis
- Time Complexity: O(|\texttt{nums1}| + |\texttt{nums2}|)
- Space Complexity: O(1)
1775. Equal Sum Arrays With Minimum Number of Operations LeetCode Solution in C++
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() * 6 < nums2.size() || nums2.size() * 6 < nums1.size())
return -1;
int sum1 = accumulate(nums1.begin(), nums1.end(), 0);
int sum2 = accumulate(nums2.begin(), nums2.end(), 0);
if (sum1 > sum2)
return minOperations(nums2, nums1);
int ans = 0;
// increasing in `nums1` and decreasing in `nums2`
vector<int> count(6);
for (const int num : nums1)
++count[6 - num];
for (const int num : nums2)
++count[num - 1];
for (int i = 5; sum2 > sum1;) {
while (count[i] == 0)
--i;
sum1 += i;
--count[i];
++ans;
}
return ans;
}
};
/* code provided by PROGIEZ */
1775. Equal Sum Arrays With Minimum Number of Operations LeetCode Solution in Java
class Solution {
public int minOperations(int[] nums1, int[] nums2) {
if (nums1.length * 6 < nums2.length || nums2.length * 6 < nums1.length)
return -1;
int sum1 = Arrays.stream(nums1).sum();
int sum2 = Arrays.stream(nums2).sum();
if (sum1 > sum2)
return minOperations(nums2, nums1);
int ans = 0;
// increasing in `nums1` and decreasing in `nums2`
int[] count = new int[6];
Arrays.stream(nums1).forEach(num -> ++count[6 - num]);
Arrays.stream(nums2).forEach(num -> ++count[num - 1]);
for (int i = 5; sum2 > sum1;) {
while (count[i] == 0)
--i;
sum1 += i;
--count[i];
++ans;
}
return ans;
}
}
// code provided by PROGIEZ
1775. Equal Sum Arrays With Minimum Number of Operations LeetCode Solution in Python
N/A
# 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.