Math with Lego Blocks

Reported in interviews at Citadel

Problem statement

A math teacher lays out two rows of Lego blocks, rowA (length n) and rowB (length m). Each block shows a positive integer, but some values are missing and are written as 0. You must replace every 0 with a positive integer (at least 1) so that the sum of rowA equals the sum of rowB. Return the minimum possible value of that common sum, or -1 if it cannot be done.

Implement:

def findMinimumEqualSum(rowA, rowB):

It returns an integer: the minimum equal sum, or -1 if no replacement makes the sums equal.

Example: rowA = [1, 0, 2], rowB = [1, 3, 0, 0]. Replacing zeros gives rowA = [1, 3, 2] (sum 6) and rowB = [1, 3, 1, 1] (sum 6); 6 is the minimum achievable equal sum, so the answer is 6.

More examples: rowA = [2, 5, 0, 1, 1], rowB = [2, 1, 0, 0] -> 10; rowA = [0, 0, 0], rowB = [1, 1] -> -1 (rowA's three zeros force a sum of at least 3, but rowB is fixed at 2 with no zeros to raise it).

Constraints: $1 \le n, m \le 10^5$, $0 \le \text{rowA}[i], \text{rowB}[i] \le 10^4$.

Show hint

What range of sums can a single row reach once you account for how many blanks it has?

Relevant roles: ML Engineer, Forward Deployed Engineer and AI Engineer.

Related free problems