Minimum Equal Sum of Two Arrays After Replacing Zeros
MediumUpdated: Aug 2, 2025
Practice on:
Problem
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.
Examples
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 <= 10^50 <= nums1[i], nums2[i] <= 10^6
Solution
Method 1 - Greedy
The goal is to minimise the equal sum of two arrays after replacing 0s with strictly positive integers (1 in this case). The approach is straightforward:
- Initial Sum Calculation:
- Compute the sum of elements in both arrays (
sum1andsum2). - Count the number of
0s in both arrays (zero1andzero2).
- Compute the sum of elements in both arrays (
- Handle Replacement:
- Replace
0s with1during sum computation. - Adjust the counts
zero1andzero2while calculating the adjusted sums.
- Replace
- Impossibility Check:
- If one array has no
0s and its sum cannot match the other array's sum (because it cannot be increased or decreased further), return-1.
- If one array has no
- Result:
- Return the maximum possible sum after adjustments since both sums will now be identical.
Code
Java
class Solution {
public long minSum(int[] nums1, int[] nums2) {
long sum1 = 0, sum2 = 0;
long zeroCount1 = 0, zeroCount2 = 0;
// Calculate sums and count zeros in nums1
for (int num : nums1) {
if (num == 0) {
sum1 += 1; // Replace 0 with 1
zeroCount1++;
} else {
sum1 += num;
}
}
// Calculate sums and count zeros in nums2
for (int num : nums2) {
if (num == 0) {
sum2 += 1; // Replace 0 with 1
zeroCount2++;
} else {
sum2 += num;
}
}
// Check if it's impossible to equalise sums
if ((zeroCount1 == 0 && sum2 > sum1) || (zeroCount2 == 0 && sum1 > sum2)) {
return -1;
}
// Return the maximum adjusted sum (both sums are equalised)
return Math.max(sum1, sum2);
}
}
Python
class Solution:
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
sum1, sum2 = 0, 0
zero_count1, zero_count2 = 0, 0
# Calculate sums and count zeros in nums1
for num in nums1:
if num == 0:
sum1 += 1 # Replace 0 with 1
zero_count1 += 1
else:
sum1 += num
# Calculate sums and count zeros in nums2
for num in nums2:
if num == 0:
sum2 += 1 # Replace 0 with 1
zero_count2 += 1
else:
sum2 += num
# Check if it's impossible to equalise sums
if (zero_count1 == 0 and sum2 > sum1) or (zero_count2 == 0 and sum1 > sum2):
return -1
# Return the maximum adjusted sum (both sums are equalised)
return max(sum1, sum2)
Complexity
- ⏰ Time complexity:
O(n + m). Iterating through both arrays once:O(n + m)wherenandmrepresent the lengths ofnums1andnums2. - 🧺 Space complexity:
O(1). Constant space usage:O(1)since no additional data structures are used.