All problems
0850MediumArrayHash TableGreedyCounting

Fewest Redials to Level Two Tallies

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1775Equal Sum Arrays With Minimum Number of Operations

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Two dice trays are logged as nums1 and nums2, and every entry is a face value from 1 to 6.

One redial picks a single entry in either tray and sets it to any face value from 1 to 6. Return the fewest redials that make the two trays total the same amount, or -1 when no number of redials can. A redial count is never negative, so -1 can only mean it is impossible.

Examples

Example 1

Input
nums1 = [1, 2, 3, 4, 5, 6], nums2 = [1, 1, 2, 2, 2, 2]
Output
3

The trays total 21 and 10, a gap of 11. Setting the 1 of the second tray to 6 and one of its 2s to 6 raises it to 19, and dropping the 6 of the first tray to 1 lowers that tray to 16. Three redials leave both at the same amount.

Example 2

Input
nums1 = [1, 1, 1, 1, 1, 1, 1, 1], nums2 = [6]
Output
-1

The first tray totals at least 8 whatever is redialled, while the second can never exceed 6, so the two can never be levelled.

Example 3

Input
nums1 = [3], nums2 = [3]
Output
0

The trays already total the same amount, so no redial is needed.

Constraints

  • 1 <= nums1.length <= 10^5
  • 1 <= nums2.length <= 10^5
  • 1 <= nums1[i] <= 6
  • 1 <= nums2[i] <= 6

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fewest_redials(nums1: list[int], nums2: list[int]) -> int:
Java
public int fewestRedials(int[] nums1, int[] nums2)
September 7
Apply