I wonder if someone has a solution for this problem : Given two lists of different size, find a solution to have no more zeros in two lists, and the sum of the two becomes equal.
Input : Two lists of size n,m >= 0 Output : The two new lists, if it is not possible return -1
Example : l1 = [1,2,0,0,4] l2 = [3,0,7] Returns : l3 = [1,2,2,2,4], l4 = [3,1,7]
We have sum(l3) = sum(l4) and no more zeros in both list
I assumed that the numbers replacing the 0s are integers between 1-9... otherwise infinite-many solutions.
Idea:
sum(l1_new) == sum(l1_new)
means thatsum(l1)+o1 == sum(l2)+o2
whereo1
ando2
are the integers made of the partitions of the 0s. The sums of the old and new lists are constant and by rearranging the equation we get a linear relationshipo2 = s1 -s2 + o1
. Botho1
ando2
are subject to constraints depending on the amount of 0s per list (boundaries).Finally use combination with replacements in order to include cases such as
3 = 1+1+1
where1
... is repeated. If this condition is too relaxed useitertools.combinations
.Here some tests
Output