> For the complete documentation index, see [llms.txt](https://zhenchaogan.gitbook.io/leetcode-solution/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-1775-equal-sum-arrays-with-minimum-number-of-operations.md).

# LeetCode. 1775 Equal Sum Arrays With Minimum Number of Operations

You are given two arrays of integers `nums1` and `nums2`, possibly of different lengths. The values in the arrays are between `1` and `6`, inclusive.

In one operation, you can change any integer's value in **any** of the arrays to **any** value between `1` and `6`, inclusive.

Return *the minimum number of operations required to make the sum of values in* `nums1` *equal to the sum of values in* `nums2`*.* Return `-1`​​​​​ if it is not possible to make the sum of the two arrays equal.

**Example 1:**

```
Input: nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
Output: 3
Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
- Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2].
- Change nums1[5] to 1. nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2].
- Change nums1[2] to 2. nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2].
```

**Example 2:**

```
Input: nums1 = [1,1,1,1,1,1,1], nums2 = [6]
Output: -1
Explanation: There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
```

**Example 3:**

```
Input: nums1 = [6,6], nums2 = [1]
Output: 3
Explanation: You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. 
- Change nums1[0] to 2. nums1 = [2,6], nums2 = [1].
- Change nums1[1] to 2. nums1 = [2,2], nums2 = [1].
- Change nums2[0] to 4. nums1 = [2,2], nums2 = [4].
```

**Constraints:**

* `1 <= nums1.length, nums2.length <= 10^5`
* `1 <= nums1[i], nums2[i] <= 6`

## Solution:

[English Version in Youtube](https://youtu.be/HkMyTGrBQxU)

[中文版解答Youtube Link](https://youtu.be/b-5bi-GA8_o)

[中文版解答Bilibili Link](https://www.bilibili.com/video/BV1254y1h7ao/)

```
class Solution {
    
    int process(int* table1, int* table2, int diff) {
        int ans = 0;
        for (int k = 1; k <= 5 && diff > 0; k++) {
            int contrib = 6 - k;
            
            while (table2[k] > 0 && diff > 0) {
                diff -= contrib;
                ans++;
                table2[k]--;
            }
            while (table1[7 - k] > 0 && diff > 0) {
                diff -= contrib;
                ans++;
                table1[7 - k]--;
            }
        }
        
        return ans;
    }
    
public:
    int minOperations(vector<int>& nums1, vector<int>& nums2) {
        if (6 * nums1.size() < nums2.size()) return -1;
        if (6 * nums2.size() < nums1.size()) return -1;
        int sum1 = 0;
        int table1[7] = {0};
        int sum2 = 0;
        int table2[7] = {0};
        
        for (int num : nums1) {
            sum1 += num;
            table1[num]++;
        }
        for (int num : nums2) {
            sum2 += num;
            table2[num]++;
        }
        
        if (sum1 == sum2) return 0;
        else if (sum1 > sum2) return process(table1, table2, sum1 - sum2);
        else return process(table2, table1, sum2 - sum1);
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-1775-equal-sum-arrays-with-minimum-number-of-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
