Skip to content

3171. Minimum Equal Sum Of Two Arrays After Replacing Zeros

Difficulty: Medium

LeetCode Problem View on GitHub


3171. Minimum Equal Sum of Two Arrays After Replacing Zeros

Medium


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

Solution

class Solution {
    public long minSum(int[] nums1, int[] nums2) {
        int n = nums1.length, m = nums2.length;
        long count1 = 0, count2 = 0;
        long sum1 = 0, sum2 = 0;
        for (int ele : nums1) {
            sum1 += ele;
            if (ele == 0) count1++;
        } 
        for (int ele : nums2) {
            sum2 += ele;
            if (ele == 0) count2++;
        }
        if (sum1 > sum2) {
            long diff = sum1 - sum2;
            if (count2 == 0) return -1;
            if (count1 == 0 && sum2 + count2 > sum1) return -1;
            else {
                return (Math.max(sum1 + count1, sum2 + count2));
            }
        }
        else if (sum2 > sum1) {
            long diff = sum2 - sum2;
            if (count1 == 0) return -1;
            if (count2 == 0 && sum1 + count1 > sum2) return -1;
            else {
                return (Math.max(sum2 + count2, sum1 + count1));
            }
        }
        else if (sum1 == sum2) {
            if (count1 == 0 && count2 == 0) return sum1;
            else if (count1 > 0 && count2 == 0) return -1;
            else if (count2 > 0 && count1 == 0) return -1;
            return (Math.max(sum1 + count1, sum2 + count2));
        }
        return -1;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here