Equal Sum Arrays With Minimum Number of Operations
Real-world version: Fewest Redials to Level Two TalliesThe 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.
def fewest_redials(nums1: list[int], nums2: list[int]) -> int:public int fewestRedials(int[] nums1, int[] nums2)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium
- Minimum Operations to Make the Array AlternatingReal-world version: Fewest Changes to Make the Strip AlternateMedium
- Majority ElementReal-world version: Quorum Drift ValueEasy