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

Problem Statement of Minimum Equal Sum of Two Arrays After Replacing Zeros
You are given two arrays nums1 and nums2 consisting of positive integers.
You have to replace all the 0’s in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.
Return the minimum equal sum you can obtain, or -1 if it is impossible.
Example 1:
Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output: 12
Explanation: We can replace 0’s in the following way:
– Replace the two 0’s in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
– Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
Example 2:
Input: nums1 = [2,0,2,0], nums2 = [1,4]
Output: -1
Explanation: It is impossible to make the sum of both arrays equal.
Constraints:
1 <= nums1.length, nums2.length <= 105
0 <= nums1[i], nums2[i] <= 106
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2918. Minimum Equal Sum of Two Arrays After Replacing Zeros LeetCode Solution in C++
class Solution {
public:
long long minSum(vector<int>& nums1, vector<int>& nums2) {
const long sum1 = accumulate(nums1.begin(), nums1.end(), 0L);
const long sum2 = accumulate(nums2.begin(), nums2.end(), 0L);
const int zero1 = ranges::count(nums1, 0);
const int zero2 = ranges::count(nums2, 0);
if (zero1 == 0 && sum1 < sum2 + zero2)
return -1;
if (zero2 == 0 && sum2 < sum1 + zero1)
return -1;
return max(sum1 + zero1, sum2 + zero2);
}
};
/* code provided by PROGIEZ */
2918. Minimum Equal Sum of Two Arrays After Replacing Zeros LeetCode Solution in Java
class Solution {
public long minSum(int[] nums1, int[] nums2) {
final long sum1 = Arrays.stream(nums1).asLongStream().sum();
final long sum2 = Arrays.stream(nums2).asLongStream().sum();
final long zero1 = Arrays.stream(nums1).filter(num -> num == 0).count();
final long zero2 = Arrays.stream(nums2).filter(num -> num == 0).count();
if (zero1 == 0 && sum1 < sum2 + zero2)
return -1;
if (zero2 == 0 && sum2 < sum1 + zero1)
return -1;
return Math.max(sum1 + zero1, sum2 + zero2);
}
}
// code provided by PROGIEZ
2918. Minimum Equal Sum of Two Arrays After Replacing Zeros LeetCode Solution in Python
class Solution:
def minSum(self, nums1: list[int], nums2: list[int]) -> int:
sum1 = sum(nums1)
sum2 = sum(nums2)
zero1 = nums1.count(0)
zero2 = nums2.count(0)
if zero1 == 0 and sum1 < sum2 + zero2:
return -1
if zero2 == 0 and sum2 < sum1 + zero1:
return -1
return max(sum1 + zero1, sum2 + zero2)
# 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.