Minimum Equal Sum of Two Arrays After Replacing Zeros

medium array greedy math

Problem

Given two arrays nums1 and nums2 of positive integers, replace every 0 in both arrays with a strictly positive integer so the two arrays end up with an equal sum. Return the minimum such equal sum, or -1 if it is impossible.

Each zero must become at least 1, so the smallest sum a side can reach is its current sum plus its count of zeros. A side with no zeros is locked at its current sum and cannot grow.

Inputnums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output12
nums1 has sum 6 with two zeros (floor 8); nums2 has sum 11 with one zero (floor 12). The larger floor wins: 12. Fill the zeros, e.g. nums1 = [3,2,2,1,4], nums2 = [6,5,1].

def minSum(nums1, nums2):
    # sum and zero-count of each array
    sum1 = sum(nums1); zero1 = nums1.count(0)
    sum2 = sum(nums2); zero2 = nums2.count(0)
    # each 0 must become >= 1, so the smallest reachable sum is sum + zeros
    low1 = sum1 + zero1
    low2 = sum2 + zero2
    # a side with no zeros is fixed; if it must still grow, it is impossible
    if zero1 == 0 and low1 < low2:
        return -1
    if zero2 == 0 and low2 < low1:
        return -1
    # otherwise lift the smaller side up to the larger floor
    return max(low1, low2)
function minSum(nums1, nums2) {
  // sum and zero-count of each array
  let sum1 = 0, zero1 = 0, sum2 = 0, zero2 = 0;
  for (const v of nums1) { sum1 += v; if (v === 0) zero1++; }
  for (const v of nums2) { sum2 += v; if (v === 0) zero2++; }
  // each 0 must become >= 1, so the smallest reachable sum is sum + zeros
  const low1 = sum1 + zero1;
  const low2 = sum2 + zero2;
  // a side with no zeros is fixed; if it must still grow, it is impossible
  if (zero1 === 0 && low1 < low2) return -1;
  if (zero2 === 0 && low2 < low1) return -1;
  // otherwise lift the smaller side up to the larger floor
  return Math.max(low1, low2);
}
long minSum(int[] nums1, int[] nums2) {
    // sum and zero-count of each array
    long sum1 = 0, sum2 = 0;
    int zero1 = 0, zero2 = 0;
    for (int v : nums1) { sum1 += v; if (v == 0) zero1++; }
    for (int v : nums2) { sum2 += v; if (v == 0) zero2++; }
    // each 0 must become >= 1, so the smallest reachable sum is sum + zeros
    long low1 = sum1 + zero1;
    long low2 = sum2 + zero2;
    // a side with no zeros is fixed; if it must still grow, it is impossible
    if (zero1 == 0 && low1 < low2) return -1;
    if (zero2 == 0 && low2 < low1) return -1;
    // otherwise lift the smaller side up to the larger floor
    return Math.max(low1, low2);
}
long long minSum(vector<int>& nums1, vector<int>& nums2) {
    // sum and zero-count of each array
    long long sum1 = 0, sum2 = 0;
    int zero1 = 0, zero2 = 0;
    for (int v : nums1) { sum1 += v; if (v == 0) zero1++; }
    for (int v : nums2) { sum2 += v; if (v == 0) zero2++; }
    // each 0 must become >= 1, so the smallest reachable sum is sum + zeros
    long long low1 = sum1 + zero1;
    long long low2 = sum2 + zero2;
    // a side with no zeros is fixed; if it must still grow, it is impossible
    if (zero1 == 0 && low1 < low2) return -1;
    if (zero2 == 0 && low2 < low1) return -1;
    // otherwise lift the smaller side up to the larger floor
    return max(low1, low2);
}
Time: O(n + m) Space: O(1)