-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path2918.minimum-equal-sum-of-two-arrays-after-replacing-zeros.cpp
More file actions
48 lines (45 loc) · 1.59 KB
/
2918.minimum-equal-sum-of-two-arrays-after-replacing-zeros.cpp
File metadata and controls
48 lines (45 loc) · 1.59 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Tag: Array, Greedy
// Time: O(N)
// Space: O(1)
// Ref: -
// Note: -
// Video: https://youtu.be/o5nHLgCQXG8
// 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
//
//
class Solution {
public:
long long minSum(vector<int>& nums1, vector<int>& nums2) {
long long zero1 = count(nums1.begin(), nums1.end(), 0);
long long zero2 = count(nums2.begin(), nums2.end(), 0);
long long s1 = accumulate(nums1.begin(), nums1.end(), zero1);
long long s2 = accumulate(nums2.begin(), nums2.end(), zero2);
if ((s1 < s2 && zero1 == 0) || (s2 < s1 && zero2 == 0)) {
return -1;
}
return max(s1, s2);
}
};