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:

1
2
3
4
5
6
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:

1
2
3
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

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:

  1. Initial Sum Calculation:
    • Compute the sum of elements in both arrays (sum1 and sum2).
    • Count the number of 0s in both arrays (zero1 and zero2).
  2. Handle Replacement:
    • Replace 0s with 1 during sum computation.
    • Adjust the counts zero1 and zero2 while calculating the adjusted sums.
  3. 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.
  4. Result:
    • Return the maximum possible sum after adjustments since both sums will now be identical.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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) where n and m represent the lengths of nums1 and nums2.
  • 🧺 Space complexity: O(1). Constant space usage: O(1) since no additional data structures are used.