FastPrepFastPrep
Problem Brief

Math with Lego Blocks

FULLTIMEOA

To make learning more interactive and fun for students, a math teacher decides to teach a concept to students by using Lego blocks. There are 2 rows of legos, rowA (of length n) and rowB (of length m). Both rows hold legos with positive integer values printed on them. However, some values (possibly, none) are missing. The missing values are denoted by 0. Students need to incorporate the missing values. The task is to replace each 0 with a positive integer such that the sums of both arrays are equal. Return the minimum sum possible. If it is not possible to make the sums equal, return -1.

Function Description

Complete the function findMinimumEqualSum in the editor.

findMinimumEqualSum has the following parameters:

  1. 1. int rowA[n]: one row of integers
  2. 2. int rowB[m]: another row of integers

Returns

int: an integer, which, if positive, denotes the minimum equal sum, and if -1 indicates that it is not possible to obtain an equal sum.

1Example 1

Input
rowA = [2, 5, 0, 1, 1], rowB = [2, 1, 0, 0]
Output
10
Explanation
Replace the zero in rowA with 1 so that rowA = [2, 5, 1, 1, 1]. Replace the zeroes in rowB with 3 and 4 so that rowB = [2, 1, 3, 4].

2Example 2

Input
rowA = [1, 0, 2], rowB = [1, 3, 0, 0]
Output
6
Explanation
Example 2 illustration
After replcing the 0s in the rows: rowA = [1, 3, 2] rowB = [1, 3, 1, 1] The sum of elements in rowA is 1 + 3 + 2 = 6 and the sum of elements in rowB is 1 + 3 + 1 + 1 = 6. This is the min possible equal sum that can be achieved. Hence, the answer is 6.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n, m ≤ 10^5
  • 0 ≤ rowA[i], rowB[j] ≤ 10^4
public int findMinimumEqualSum(int[] rowA, int[] rowB) {
    // write your code here
}
Input

rowA

[2, 5, 0, 1, 1]

rowB

[2, 1, 0, 0]

Output

10

Sign in to submit your solution.