Minimum Equal Sum of Two Arrays After Replacing Zeros - 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.
Input & Output
Example 1 — Basic Case with Zeros
$
Input:
nums1 = [3,2,0,1,0], nums2 = [6,5,0]
›
Output:
12
💡 Note:
Replace zeros with 1: nums1 = [3,2,1,1,1] (sum=8), nums2 = [6,5,1] (sum=12). Since both arrays have zeros, we can make nums1's zeros larger to match nums2's sum. The minimum equal sum is max(8,12) = 12.
Example 2 — Impossible Case
$
Input:
nums1 = [1,2], nums2 = [3,4]
›
Output:
-1
💡 Note:
No zeros in either array. nums1 sum = 3, nums2 sum = 7. Since we can't change any values, it's impossible to make them equal.
Example 3 — One Array Without Zeros
$
Input:
nums1 = [1,1,1,1], nums2 = [0,0,0,0]
›
Output:
4
💡 Note:
nums1 sum = 4 (no zeros), nums2 can be [1,1,1,1] with sum = 4. Both sums can be equal at 4.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 105
- 0 ≤ nums1[i], nums2[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code