Skip to content

Commit 1985b19

Browse files
authored
Create Minimum Equal Sum of Two Arrays After Replacing Zeros.py
1 parent 411b7d4 commit 1985b19

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
You are given two arrays nums1 and nums2 consisting of positive integers.
3+
4+
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.
5+
6+
Return the minimum equal sum you can obtain, or -1 if it is impossible.
7+
'''
8+
-----------------------------------------------------------------------------------------------------------------------------------------------------
9+
class Solution:
10+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
11+
res = -1
12+
13+
s1 = sum(nums1)
14+
s2 = sum(nums2)
15+
16+
if s1 > s2:
17+
diff = s1-s2
18+
else:
19+
diff = s2-s1
20+
21+
rem = diff
22+
if (nums1.count(0) > 0 and nums2.count(0) == 0 and rem!=0) or (nums1.count(0)== 0 and nums2.count(0)>0 and rem!=0):
23+
return -1
24+
25+
nums1.sort()
26+
nums2.sort()
27+
28+
p1 = 0
29+
p2 = 0
30+
31+
return diff
32+
33+
34+
35+
-------------------------------------------
36+
class Solution:
37+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
38+
sum1, zero1=sum(nums1), nums1.count(0)
39+
sum2, zero2=sum(nums2), nums2.count(0)
40+
if (zero1==0 and sum1<sum2+zero2)or(zero2==0 and sum2<sum1+zero1):
41+
return -1
42+
return max(sum1+zero1, sum2+zero2)
43+
44+
45+

0 commit comments

Comments
 (0)