Equalize Two Dice-Valued Array Sums

Quick Overview

Equalize Two Dice-Valued Array Sums. Each entry in nums1 and nums2 is an integer from 1 through 6. One operation changes any entry of either array to any integer in that range. The arrays may have different lengths.

Equalize Two Dice-Valued Array Sums

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each entry in `nums1` and `nums2` is an integer from 1 through 6. One operation changes any entry of either array to any integer in that range. The arrays may have different lengths. Return the smallest operation count that makes their sums equal, or `-1` if impossible. ```cpp class Solution { public: int minOperations(vector<int>& nums1, vector<int>& nums2); }; ``` ### Examples **Example 1:** ```text nums1 = [1,1], nums2 = [6] Output: 1 Explanation: Change 6 to 2, or increase one 1 to 5. ``` **Example 2:** ```text nums1 = [1,1,1,1,1,1,1,1], nums2 = [2] Output: -1 Explanation: The first sum is at least 8 while the second is at most 6. ``` ### Constraints - Each array length is in `[1,10^5]`. - Every value in either array lies in `[1,6]`. ### Clarifying Questions One operation may change a value by more than one. The last change may use less than its maximum available improvement. ```hint Inspect the structure What values may an operation assign, and how can the arrays' different lengths affect feasibility? ``` ### Reasoning checkpoints - Respect the `[1,6]` range and distinguish feasibility from minimum operation count. - Prove that the reported number of changes can equalize the sums and that fewer cannot. ### Optional extensions Why are repeated changes to the same entry unnecessary in an optimal solution?

Overview: Equalize Two Dice-Valued Array Sums. Each entry in nums1 and nums2 is an integer from 1 through 6. One operation changes any entry of either array to any integer in that range. The arrays may have different lengths.

Infosys logo
Infosys
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each entry in nums1 and nums2 is an integer from 1 through 6. One operation changes any entry of either array to any integer in that range. The arrays may have different lengths. Return the smallest operation count that makes their sums equal, or -1 if impossible.

class Solution {
public:
    int minOperations(vector<int>& nums1, vector<int>& nums2);
};

Examples

Example 1:

nums1 = [1,1], nums2 = [6]
Output: 1
Explanation: Change 6 to 2, or increase one 1 to 5.

Example 2:

nums1 = [1,1,1,1,1,1,1,1], nums2 = [2]
Output: -1
Explanation: The first sum is at least 8 while the second is at most 6.

Constraints

  • Each array length is in [1,10^5] .
  • Every value in either array lies in [1,6] .

Clarifying Questions Guidance

One operation may change a value by more than one. The last change may use less than its maximum available improvement.

Reasoning checkpoints

  • Respect the [1,6] range and distinguish feasibility from minimum operation count.
  • Prove that the reported number of changes can equalize the sums and that fewer cannot.

Optional extensions

Why are repeated changes to the same entry unnecessary in an optimal solution?