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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem is all about trying every possible combination of numbers to replace the zeros in both lists. Then, for each possible combination, we check if the lists have the same total, and if so, whether that total meets the problem requirements.
Here's how the algorithm would work step-by-step:
def minimum_equal_sum_brute_force(first_list, second_list):
possible_sums = []
first_zero_indices = [i for i, number in enumerate(first_list) if number == 0]
second_zero_indices = [i for i, number in enumerate(second_list) if number == 0]
def generate_combinations(list_to_modify, zero_indices, index, current_combination):
nonlocal possible_sums
if index == len(zero_indices):
modified_list = list_to_modify[:]
list_sum = sum(modified_list)
return list_sum, modified_list
else:
list_sum = None
modified_list = None
return list_sum, modified_list
def iterate_replacements(first_list, second_list):
nonlocal possible_sums
import itertools
first_zero_indices_count = first_list.count(0)
second_zero_indices_count = second_list.count(0)
#Creates all possible combinations from 1 to the sum of the lists
for first_replacement in itertools.product(range(1, sum(first_list + second_list) +1), repeat = first_zero_indices_count):
for second_replacement in itertools.product(range(1, sum(first_list + second_list)+1), repeat = second_zero_indices_count):
first_list_copy = first_list[:]
second_list_copy = second_list[:]
first_replacement_index = 0
for i in range(len(first_list_copy)):
if first_list_copy[i] == 0:
first_list_copy[i] = first_replacement[first_replacement_index]
first_replacement_index += 1
second_replacement_index = 0
for i in range(len(second_list_copy)):
if second_list_copy[i] == 0:
second_list_copy[i] = second_replacement[second_replacement_index]
second_replacement_index += 1
#Check if list sums are equal
if sum(first_list_copy) == sum(second_list_copy):
#Must ensure that all zeros are replaced by at least one.
if all(number > 0 for number in first_list_copy) and all(number > 0 for number in second_list_copy):
possible_sums.append(sum(first_list_copy))
iterate_replacements(first_list, second_list)
if not possible_sums:
return -1
else:
return min(possible_sums)
The goal is to make the sums of the two groups of numbers equal by changing some zeros into other numbers. We want to do this in the simplest way possible. The strategy is to figure out the minimum sum each group can achieve, and then compare them to see if we can reach the same total.
Here's how the algorithm would work step-by-step:
def min_impossible(first_array, second_array):
first_array_sum = sum(first_array)
second_array_sum = sum(second_array)
first_array_zeros = first_array.count(0)
second_array_zeros = second_array.count(0)
first_array_min_sum = first_array_sum + first_array_zeros
second_array_min_sum = second_array_sum + second_array_zeros
if first_array_min_sum < second_array_min_sum:
# If first is smaller, check if its possible to equalize
if first_array_zeros == 0:
return -1
else:
return second_array_min_sum
elif second_array_min_sum < first_array_min_sum:
# If second is smaller, check if its possible to equalize
if second_array_zeros == 0:
return -1
else:
return first_array_min_sum
else:
# The minimum sums are equal, so it is possible
return first_array_min_sum
Case | How to Handle |
---|---|
Both arrays are empty | Return -1 since no sums can be computed. |
One array is empty, the other is not | Return -1 since a sum from the empty array is impossible. |
Arrays contain only zeros | The minimum equal sum is achievable; fill all zeros with 1 and check if resulting sums are equal, and return if they are, or return -1 if they are not. |
Arrays contain only non-zero positive integers | If the sums of the initial arrays are not equal, it is impossible to make them equal by replacing zeros, so return -1. |
Arrays contain a very large number of zeros | Check for potential integer overflows when calculating sums after replacing zeros with the minimum possible values (1). |
One array sum can never reach the other array sum, even when replacing all zeros with very large integers. | Detect this case and return -1 early to avoid unnecessary computations. |
Integer overflow when calculating the sum of an array. | Use a larger data type (e.g., long) to store the sums to prevent integer overflow during calculations. |
One or both arrays contain only zeros, and both are very long. | Since all zero values will be converted to 1, just compare array lengths, and if equal return the value which is the array length; otherwise return -1. |