Show problems as
Classic problem #1775MediumArrayHash TableGreedyCounting

Equal Sum Arrays With Minimum Number of Operations

Real-world version: Fewest Redials to Level Two Tallies
Tracked in this browser only
Read Equal Sum Arrays With Minimum Number of Operations on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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