Math with Lego Blocks
Updated
Problem statement
A teacher sets out two rows of Lego blocks for a math lesson. The first row is the array nums1 of length n, and the second row is the array nums2 of length m. Each block is meant to carry a positive integer, but some numbers have gone missing; a block with a missing number shows 0. A row may have no missing numbers at all.
The students must write a positive integer on every block that shows 0, picking the numbers so that the blocks of nums1 add up to the same total as the blocks of nums2. Different blocks may get different numbers.
Return the smallest total the two rows can share once every 0 has been filled in. If no choice of numbers can make the two totals match, return -1.
Function
minSum(nums1: int[], nums2: int[]) → longExamples
Example 1
nums1 = [1, 0, 2]nums2 = [1, 3, 0, 0]return = 6Counting each 0 as 1, nums1 adds up to at least 4 and nums2 to at least 6. Writing 3 on the blank block of nums1 and 1 on both blank blocks of nums2 gives [1, 3, 2] and [1, 3, 1, 1], which both add up to 6. No smaller shared total exists, because nums2 never adds up to less than 6.
Example 2
nums1 = [5, 0, 0]nums2 = [3, 2]return = -1The two blank blocks of nums1 get at least 1 each, so nums1 adds up to at least 7. nums2 has no blank block, so its total stays 5. Since nums1 can never drop to 5, the totals cannot match.
Example 3
nums1 = [0, 7]nums2 = [2, 0, 1, 0]return = 8nums1 adds up to at least 8 and nums2 to at least 5. Writing 1 on the blank block of nums1 and 1 and 4 on the blank blocks of nums2 gives [1, 7] and [2, 1, 1, 4], which both add up to 8.
Constraints
1 <= n == nums1.length <= 1051 <= m == nums2.length <= 1050 <= nums1[i], nums2[j] <= 104
More Patreon interview questions
- Three SumOA · Seen Apr 2025
- Make Layer Times OddOA · Seen Oct 2024
- Sum of Subarray RegionsOA · Seen Oct 2024
- Binary Cardinality SortingOA · Seen Apr 2023
- Closest NumbersOA · Seen Apr 2023
- Maximum Books From BundlesOA · Seen Apr 2023
- Professors Teaching Outside Their DepartmentOA · Seen Apr 2023
- Botany QueryOA · Seen Apr 2023
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
Similar questions at other companies
- Minimum Non-Adjacent Selection CapabilityMicrosoft · OAMicrosoft interview questions
- Earn the Most 🐝Capital One · OACapital One interview questions
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Minimum Cost of Left and Right PropagationAmazon · OA
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA