2918. 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.
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
| class Solution { public: long long minSum(vector<int>& nums1, vector<int>& nums2) { long long sum1 = 0, sum2 = 0, c1 = 0, c2 = 0; for(auto& n : nums1) { sum1 += n; if(n == 0) c1 += 1; } for(auto& n : nums2) { sum2 += n; if(n == 0) c2 += 1; } if(c1 == 0 and c2 == 0) { if(sum1 == sum2) return sum1; return -1; } if(c1 == 0) { if(sum2 + c2 <= sum1) return sum1; return -1; } if(c2 == 0) { if(sum1 + c1 <= sum2) return sum2; return -1; } return max(sum1 + c1, sum2 + c2); } };
|