Ctrl K

Balance the Dice Rows

Medium
Two rows of dice lie on a table: left and right hold the values on their top faces (1 to 6). The rows may have different numbers of dice. In one move you can pick any die in either row and turn it to show any face, which replaces its value with any number from 1 to 6. Return the smallest number of moves that makes the two rows add up to the same total, or -1 if that cannot be done.
Example 1
left=[2, 2], right=[6] → 1

The sums are 4 and 6. Turning the right die to show 4 is enough.

Example 2
left=[1, 2], right=[6, 6] → 2

The sums are 3 and 12, a gap of 9. One turn moves a sum by at most 5, so at least two are needed: for example, turn the left 1 into a 6 and one right 6 into a 2, giving 8 and 8.

Example 3
left=[1, 1, 1, 1, 1, 1, 1, 1], right=[5] → -1

The left sum can never drop below 8, and the right sum can never exceed 6, so they cannot be made equal.

Constraints
  • 1 ≤ len(left), len(right) ≤ 10⁵
  • 1 ≤ left[i], right[i] ≤ 6
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumSpread Out the Labels MediumPick Within Tag Limits MediumFewest Kinds After Removals MediumPalindrome From Two-Letter Cards Medium
loading Python…
Input
left=[2, 2], right=[6]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 140 tests on the server, hidden ones included.