Mid/SeniorArrayGreedy

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
Inputnums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output12
By replacing the two 0's in nums1 with 2 and 4, and the 0 in nums2 with 1, both arrays have an equal sum of 12, which is the minimum possible.
Example 2
Inputnums1 = [2,0,2,0], nums2 = [1,4]
Output-1
It is impossible to make the sum of both arrays equal.

Constraints

  • 1 <= nums1.length, nums2.length <= 10^5
  • 0 <= nums1[i], nums2[i] <= 10^6

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate