Algorithms
Medium
2918
3171
Minimum Equal Sum of Two Arrays After Replacing Zeros
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:

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:
    def minSum(self, nums1: List[int], nums2: List[int]) -> int:
        
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


Please complete the code below to solve above prblem:
```python
class Solution:
    def minSum(self, nums1: List[int], nums2: List[int]) -> int:
        
```

my_solution = Solution()

test_input = { "nums1": [3,2,0,1,0], "nums2": [6,5,0] }
assert my_solution.minSum(**test_input) == 12

test_input = { "nums1": [2,0,2,0], "nums2": [1,4] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,7,28,17,18], "nums2": [1,2,6,26,1,0,27,3,0,30] }
assert my_solution.minSum(**test_input) == 98

test_input = { "nums1": [8,13,15,18,0,18,0,0,5,20,12,27,3,14,22,0], "nums2": [29,1,6,0,10,24,27,17,14,13,2,19,2,11] }
assert my_solution.minSum(**test_input) == 179

test_input = { "nums1": [9,5], "nums2": [15,12,5,21,4,26,27,9,6,29,0,18,16,0,0,0,20] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,29,5,22,5,9,30,11,20,0,18,16,26,11,3,0,24,24,14,24], "nums2": [30,12,16,3,24,6,13,0,16] }
assert my_solution.minSum(**test_input) == 294

test_input = { "nums1": [9,13,0,0,12,10,0,8,0,0,5,13,0], "nums2": [8,14,11,2,27,0,0] }
assert my_solution.minSum(**test_input) == 76

test_input = { "nums1": [3,0,20,9,20,0,20,25,26,9,0,12,6,11,0,6], "nums2": [0,3,8,13,27,0,0,0,29,27,0,11,23,0,19,19,0] }
assert my_solution.minSum(**test_input) == 186

test_input = { "nums1": [25,28,13,0,14,23,14,0,3,3,12], "nums2": [24,30,0,15,20,19,18,0,23,23,0,16,26,0,29,19,16,25] }
assert my_solution.minSum(**test_input) == 307

test_input = { "nums1": [0,29,30,18,5,24,16,5,17,0,18,16,26,0,15,19,14,20,3,26], "nums2": [0,8,14,11,13,6,8,0,13] }
assert my_solution.minSum(**test_input) == 304

test_input = { "nums1": [0,17,20,17,5,0,14,19,7,8,16,18,6], "nums2": [21,1,27,19,2,2,24,21,16,1,13,27,8,5,3,11,13,7,29,7] }
assert my_solution.minSum(**test_input) == 257

test_input = { "nums1": [26,1,25,10,14,14,4,0,10,0,23], "nums2": [23,8,30,18,8,15,6,9,0,2,0,0,19,8,19,4,10] }
assert my_solution.minSum(**test_input) == 182

test_input = { "nums1": [15,10,7,16], "nums2": [8,16,2,6,4,12,6,16,24,0] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,0,0,17,0,6,2,22,12,0,25,18,1,12,19,0,0], "nums2": [0,0,0,30,4,3,13,25,9,25,3,0,1,12,2,10,4,7,30,16] }
assert my_solution.minSum(**test_input) == 198

test_input = { "nums1": [23,17], "nums2": [7,3,22,0,12] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [15,0,8,30,6,3,24,6,0,11,13,30,6,25,23,3], "nums2": [12,20,0,6,0,0,14,0,0,8,5,19,16,0,0,15] }
assert my_solution.minSum(**test_input) == 205

test_input = { "nums1": [3,25,1,13], "nums2": [19,13,10,27,10,20,27,0,3,12,16,26,0,27] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,0], "nums2": [29,28] }
assert my_solution.minSum(**test_input) == 57

test_input = { "nums1": [17,4,11,8,0,17,0,0,12,27,20,28,0,30,21,18,12], "nums2": [0,2,30,0,5,17,0,0,0,15,11,2,25,18,18] }
assert my_solution.minSum(**test_input) == 229

test_input = { "nums1": [0,17,0,7,29,10,22,27,13,8,19], "nums2": [26,23,8,14,0,17,20,4,26,15,0,9,14,0,12,10,23,16] }
assert my_solution.minSum(**test_input) == 240

test_input = { "nums1": [0,25,8,0,22,0], "nums2": [6,12,22,3,0,28,19,0,20,21,2,3] }
assert my_solution.minSum(**test_input) == 138

test_input = { "nums1": [2,17,23,16,2,0,6,12,10], "nums2": [19,11,7,16,0] }
assert my_solution.minSum(**test_input) == 89

test_input = { "nums1": [4,7,14,15,18,7,0,6,8], "nums2": [27,2,13,18,20,23,9,0,0,25,5,0,17,0,0,14] }
assert my_solution.minSum(**test_input) == 178

test_input = { "nums1": [16,0,7,19,0,0,7,26,12,0,4,0,7,0,22,12,0,26], "nums2": [7,25,0,25,18,0,6,14,0] }
assert my_solution.minSum(**test_input) == 165

test_input = { "nums1": [7,0,3,6,5,24,0,0,0,30,20,13,0,5,19,4,25,17], "nums2": [11,19,28,25,27,6,0,18,0,19,18,16,0,16,9,0,2,23,23,10] }
assert my_solution.minSum(**test_input) == 274

test_input = { "nums1": [23,19,24,0,8,19,30,0,14], "nums2": [25,17,18,6,30] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,15,8,0,4,0,21,20,0,0,18], "nums2": [16,0] }
assert my_solution.minSum(**test_input) == 91

test_input = { "nums1": [0,0,24,21,16,4,22,21], "nums2": [18,7,28,0,0,11,2,0] }
assert my_solution.minSum(**test_input) == 110

test_input = { "nums1": [4,11,17,30,11,21,21,10,2,10,7,29,21,1,0,9,15,5], "nums2": [0,0,1,7,8,0,27,20] }
assert my_solution.minSum(**test_input) == 225

test_input = { "nums1": [12,16,1], "nums2": [1,0,19,24,21,0,0,24,24,18,26,19,13,14,30,9,0,4,20] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,0,8,0], "nums2": [23,0,15,29,25] }
assert my_solution.minSum(**test_input) == 93

test_input = { "nums1": [28,25,14,10], "nums2": [0,6,16,2,0,13,0,0,4,2,16,6,18,0,8,14,10] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,0,8,30,1,0,22,0,0,1,0], "nums2": [22,6,0,13,0,23,14,27,20,4,0,11,11,25,9,22,11,17,17] }
assert my_solution.minSum(**test_input) == 255

test_input = { "nums1": [0,0,27], "nums2": [18,0,0,7,26,11,28,20,1,19] }
assert my_solution.minSum(**test_input) == 132

test_input = { "nums1": [30,6,0,8,14,0,15,0,11,13,0,8,28,8,8,0,28,0,25], "nums2": [21,8,0,10,28,2,6,3,0,20,1,2,24,12,29] }
assert my_solution.minSum(**test_input) == 208

test_input = { "nums1": [18,27,20,10,28,12,29,5,24,0,27,9,22,0,14,0,5,11], "nums2": [24,0,14,26,1,9,24,0,12,30,13,21] }
assert my_solution.minSum(**test_input) == 264

test_input = { "nums1": [0,5], "nums2": [17,12,5,6,0,13,19,7] }
assert my_solution.minSum(**test_input) == 80

test_input = { "nums1": [30,2,20,30], "nums2": [8,14,0,2,0,18,9,24,0,0,28,0,1,14,27] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,17,0,1,19,0,0,7,23,0,0,0,5,0], "nums2": [7,1,28,8,1,0,0,5,5,18,17,23] }
assert my_solution.minSum(**test_input) == 115

test_input = { "nums1": [17,4,26,28,0,0,1,0,24,5,4,6,10,8,8,16,27], "nums2": [0,18,12,0,0,22,15] }
assert my_solution.minSum(**test_input) == 187

test_input = { "nums1": [17,1,13,12,3,13], "nums2": [2,25] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [19,19,21,6,0,0,28,3], "nums2": [5,0,16] }
assert my_solution.minSum(**test_input) == 98

test_input = { "nums1": [27,17,9,23,21,18,8,27,19,3,0,0,0,0,19], "nums2": [4,7,27,21,27,0,28,0,28,19,20,14,0,12,24,3] }
assert my_solution.minSum(**test_input) == 237

test_input = { "nums1": [25,25,0,5,15,13,26,5,25,23,19,20,1,15], "nums2": [23,6] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [9,0,15,10,18,30,7,0,1,27,24,2,8,0], "nums2": [0,0,1,13,0,9,21,3,0,20] }
assert my_solution.minSum(**test_input) == 154

test_input = { "nums1": [23,7,0,4,21,20,18,3,17,18,4,0,0,13,29,17], "nums2": [0,0,18,9,0,11,21,0] }
assert my_solution.minSum(**test_input) == 197

test_input = { "nums1": [7,0,5,7,19,12,0,11,7,24,22], "nums2": [8,3,0,23,19,24,2,10,4,20,0,14] }
assert my_solution.minSum(**test_input) == 129

test_input = { "nums1": [15,23,12,0,0,1,29,24,0,5,21,9,7,6,27,11,0,19,20], "nums2": [14,25,7,18] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [26,14,0], "nums2": [0,16,0,8,14,7,0,2,0,0,10,10,7,14,0,18,11] }
assert my_solution.minSum(**test_input) == 123

test_input = { "nums1": [16,15,27,20,29], "nums2": [27,19,0,11,2,19,28,16,0,16,24,11,0,4,2,24,8] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [26,0,28,0,28,24,15,30,9,17,0,1,21,26,21,8,0,28,0,11], "nums2": [0,20,9,12,10,16,2,21,12,0,26,11,0,21,0,0,0,29,23,22] }
assert my_solution.minSum(**test_input) == 298

test_input = { "nums1": [0,0,9,22,3,14,13,26,21], "nums2": [21,9,21,28,17,6,15,11,5,14,17,22,0,24] }
assert my_solution.minSum(**test_input) == 211

test_input = { "nums1": [0,0,18,27,7,20,9,10,29], "nums2": [29,26,19,0,0,0,0,8,24] }
assert my_solution.minSum(**test_input) == 122

test_input = { "nums1": [5,0,0,0,27,22,0,0,1,9], "nums2": [20,22,5,0,0,24,22,27,15] }
assert my_solution.minSum(**test_input) == 137

test_input = { "nums1": [2,3,0,0,0,1,18,14,25,1,0,0,3,1,13,29,0,11,0,0], "nums2": [0,12,5,14,2,0,0,14,1,10,5,17,17,8,0,0,9] }
assert my_solution.minSum(**test_input) == 129

test_input = { "nums1": [22,0,16,16,27,21,13,9,15,28,0,7,21,8,28,27,26,4], "nums2": [0,16,23,0,26,4,0,13,19,0,0,0,14,18,5,14,20,0,27] }
assert my_solution.minSum(**test_input) == 290

test_input = { "nums1": [26,0,26,18,25,20,20,3,0,14,13,5,13,0,20], "nums2": [16,17,0,12,2,26,14,0,27,17,14,10,0,0,28,29,8,25,3,7] }
assert my_solution.minSum(**test_input) == 259

test_input = { "nums1": [0,25,27,23], "nums2": [28,0,12,0,24,4,14,5,16,30,26,15,6,9,28,0] }
assert my_solution.minSum(**test_input) == 220

test_input = { "nums1": [3,23,15,19,0,7,24,27,25,0,0,16,28,15], "nums2": [26,3,21,0,26,0,9,12,0,0,21,28,23,0,0,4,16,9,7] }
assert my_solution.minSum(**test_input) == 211

test_input = { "nums1": [0,2,0,11,22,0,26,0,1,0,6,0,24,2,24,19,15,12], "nums2": [9,14,0,25,24,29,17,16,24,26,1,28,27,4,11,5,14] }
assert my_solution.minSum(**test_input) == 275

test_input = { "nums1": [1,1,1], "nums2": [18] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [29,15,0,0,0,0,1,0,9,0,0], "nums2": [16,0,30,28,23,0,0,0,0,24,14,27,0,0] }
assert my_solution.minSum(**test_input) == 169

test_input = { "nums1": [27,10,0,13,25], "nums2": [24,10,18,27,3,0,23,6,6] }
assert my_solution.minSum(**test_input) == 118

test_input = { "nums1": [5,0,10,0,4,0,13,0,27,20,12,10,15,29], "nums2": [13,25,4,0,11,0,30,0,29,17,7,28,23] }
assert my_solution.minSum(**test_input) == 190

test_input = { "nums1": [29,6,30,0,25,8,12,0,25,7,2,15,12,1,5,0,0,12], "nums2": [12,14,13,0,0,24,25,22,0,5,8,28,23,6,20,3] }
assert my_solution.minSum(**test_input) == 206

test_input = { "nums1": [9,11,20,0,0,0,21,25,0,0,0,3,11,5,18], "nums2": [0,27,27,21,28,25,3,0,25,0,21,0,3,0,30,20,17] }
assert my_solution.minSum(**test_input) == 252

test_input = { "nums1": [0,0,10,4], "nums2": [9,0,22,9,22,3,16,3,9,19,0,29,3,1,0,1,8,12] }
assert my_solution.minSum(**test_input) == 169

test_input = { "nums1": [20,10,0,16,18,0,16,21,22,4,0,15,0,8], "nums2": [0,1,2,0,20] }
assert my_solution.minSum(**test_input) == 154

test_input = { "nums1": [23,24,0], "nums2": [0,0,26,27,12,18,0] }
assert my_solution.minSum(**test_input) == 86

test_input = { "nums1": [0,17,27,12,0,22], "nums2": [20,0] }
assert my_solution.minSum(**test_input) == 80

test_input = { "nums1": [1,29], "nums2": [20,0,8,11,13,17,0,18,0,2,5,3,27,11,7,17] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [19,29,0,0,1,0,0,0,0,24,18,0,24,0,11,14,16,18], "nums2": [2,0,26,8,17] }
assert my_solution.minSum(**test_input) == 182

test_input = { "nums1": [2,0,0,19,6,29], "nums2": [25,4,0,11,0,13,28,0,28,7,4,2,16,0,22] }
assert my_solution.minSum(**test_input) == 164

test_input = { "nums1": [0,0], "nums2": [26,5,7,0,1,3,0,7,0,0,5,25,26,20,0,3,20,23,18] }
assert my_solution.minSum(**test_input) == 194

test_input = { "nums1": [21,2,0,0,12,2,0,4,6,29,15,0], "nums2": [12,20,3,10,16,25,17,8,27,0,0,23,2,0,2,4,10,27] }
assert my_solution.minSum(**test_input) == 209

test_input = { "nums1": [25,29,10,12,25,26,19,6,19,10,18], "nums2": [0,0,22,2,17,0,7,23,22,18,20,0,13,22,0,0,0,13,6,8] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,0,16,23,28,20,0,22,4,1,0,0,19,0,0,3,2,28], "nums2": [20,28] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [16,14,0,11,9,7,18,2,24,0,0,11,0,0,23], "nums2": [0,0,0,7,0,24,24,6,0,0,12,18,1,0,0] }
assert my_solution.minSum(**test_input) == 140

test_input = { "nums1": [4,15,7,10,8,11,2,0,0,22,11,0,4,14,0,16,29,0,0,27], "nums2": [13,23,8,16] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [7], "nums2": [6,26,25,0,14,19,0,29,16,29,5,26,29,6,0,25,12,0,19,19] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,10,5,16,13,20,20,0,15,4,0,4,3,0,0,15,0,24,0], "nums2": [16,14,27,0,20,0,23,0,5,10,28,21,9,28,21,8,28,0,27,0] }
assert my_solution.minSum(**test_input) == 290

test_input = { "nums1": [12,14,25,12,3], "nums2": [3,26,0,21,22] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,1,6,20,13,9,28,30,0,14,6,0,25,25,24,16,2,21], "nums2": [5,3,0] }
assert my_solution.minSum(**test_input) == 243

test_input = { "nums1": [0,12,18,0,2,12,29,0,20,29,26,14], "nums2": [1,0,29,14,24] }
assert my_solution.minSum(**test_input) == 165

test_input = { "nums1": [0,21,18,13,9,0,10], "nums2": [0,22,27,1,0,0,23,23] }
assert my_solution.minSum(**test_input) == 99

test_input = { "nums1": [28,16,0,0,0,0,0,26,3,0,3,7,5,0,19,27,1,7], "nums2": [28,9,0,16,14] }
assert my_solution.minSum(**test_input) == 149

test_input = { "nums1": [9,17,6,0,24,18,14,10,14,10,0,0,12,0,3,28,25,5,0,30], "nums2": [13,11,30,30,17,27,0,24,15,0,0] }
assert my_solution.minSum(**test_input) == 230

test_input = { "nums1": [26,2,0,0,13,14,18,17,0], "nums2": [27,30,26,14,10,24,17,2,10,25,27] }
assert my_solution.minSum(**test_input) == 212

test_input = { "nums1": [9,0,26], "nums2": [0,16] }
assert my_solution.minSum(**test_input) == 36

test_input = { "nums1": [4,17,6,0,1,8,19,30,21,11,26,0,0,19,0,12], "nums2": [29,17,7,4,29,5,0,25,11,6,0,0,13,22] }
assert my_solution.minSum(**test_input) == 178

test_input = { "nums1": [1], "nums2": [10,29,23,4,0,6,23,23,0,8,29,16,7,20,15,23] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [0,20,12,15,0,1,5,4,16,13,8,8,0,28,2,9,0,12], "nums2": [13,21,9,0,11,26,0,16,0,29,7,0,0,7,4,0,28,0,2] }
assert my_solution.minSum(**test_input) == 180

test_input = { "nums1": [0,20,0,0,8,29,17,25,4,0,0,0,0,7,13,14], "nums2": [6,21,24,14,20,19,0,0,7,21,0,11,0,0,0,0,17,16,0,6] }
assert my_solution.minSum(**test_input) == 190

test_input = { "nums1": [28,25,9,0,10,19,23,21,25,8,24,25,18,5], "nums2": [0,3,0] }
assert my_solution.minSum(**test_input) == 241

test_input = { "nums1": [23,7,15,16,25,9,30,14,8,0,0,2,25,1,7,0,16,0,19], "nums2": [3,1,24,0,25,0,7,24,0,0,17,27,0] }
assert my_solution.minSum(**test_input) == 221

test_input = { "nums1": [0,14,10,29,0,5,13,0,0,1,18,0,0,0,11,3,28,0], "nums2": [30,2,24,0,0,0,14,12,23,3,17,12,14,13,0,28,29,0,21] }
assert my_solution.minSum(**test_input) == 247

test_input = { "nums1": [5,29], "nums2": [23,24] }
assert my_solution.minSum(**test_input) == -1

test_input = { "nums1": [23,2,12,27,0,5,14,0,1,6,30,0,0,2,6,0,11,0], "nums2": [1,26,27,5,0,14,28,24,2,2,15,25,7,13,9] }
assert my_solution.minSum(**test_input) == 199

test_input = { "nums1": [0,10,29,11,11,22,0,0,12,10], "nums2": [14,0,1,3,13,29,21] }
assert my_solution.minSum(**test_input) == 108

test_input = { "nums1": [0,9,22,25,28], "nums2": [0,0,0,14,19,6,0,7,19,15,0,30,19,18,11,1,0,15,10,18] }
assert my_solution.minSum(**test_input) == 208