<p>You are given two arrays of integers <code>nums1</code> and <code><font face="monospace">nums2</font></code>, possibly of different lengths. The values in the arrays are between <code>1</code> and <code>6</code>, inclusive.</p>

<p>In one operation, you can change any integer&#39;s value in <strong>any </strong>of the arrays to <strong>any</strong> value between <code>1</code> and <code>6</code>, inclusive.</p>

<p>Return <em>the minimum number of operations required to make the sum of values in </em><code>nums1</code><em> equal to the sum of values in </em><code>nums2</code><em>.</em> Return <code>-1</code>​​​​​ if it is not possible to make the sum of the two arrays equal.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 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 = [<u><strong>6</strong></u>,1,2,2,2,2].
- Change nums1[5] to 1. nums1 = [1,2,3,4,5,<strong><u>1</u></strong>], nums2 = [6,1,2,2,2,2].
- Change nums1[2] to 2. nums1 = [1,2,<strong><u>2</u></strong>,4,5,1], nums2 = [6,1,2,2,2,2].
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 6</code></li>
</ul>

class Solution:
    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
        # sort the values order
        # find the differences in total
        n = len(nums1)
        m = len(nums2)

        if min(n, m) * 6 < max(n, m) * 1:
            return -1

        nums1 = Counter(nums1)
        nums2 = Counter(nums2)

        # binary search down the total number of moves required!
        # how to find the min number of moves...
        def sum_counter(c):
            return sum(x * c[x] for x in c)

        s1 = sum_counter(nums1)
        s2 = sum_counter(nums2)

        def is_possible_freqs(n1, n2, num_moves, num_moves_1):
            # assumes n1 < n2
            maximum_n1 = sum_counter(n1)
            moves_remaining = num_moves_1
            for i in range(1, 6):
                if n1[i] > 0:
                    maximum_n1 += min(n1[i], moves_remaining) * (6 - i)
                    moves_remaining -= min(n1[i], moves_remaining)

                if moves_remaining == 0:
                    break

            minimum_n2 = sum_counter(n2)
            moves_remaining = num_moves - num_moves_1
            for i in range(6, 1, -1):
                if n2[i] > 0:
                    minimum_n2 -= min(n2[i], moves_remaining) * (i - 1)
                    moves_remaining -= min(n2[i], moves_remaining)

                if moves_remaining == 0:
                    break

            return minimum_n2 <= maximum_n1

        def is_possible(num_moves):
            if s1 < s2:
                for num_moves_1 in range(num_moves + 1):
                    if is_possible_freqs(nums1, nums2, num_moves, num_moves_1):
                        return True
                return False
            elif s1 > s2:
                for num_moves_1 in range(num_moves + 1):
                    if is_possible_freqs(nums2, nums1, num_moves, num_moves_1):
                        return True
                return False
            else:
                return True

        left = 0
        right = n+m
        while left < right:
            mid = left + (right - left) // 2

            if is_possible(mid):
                right = mid
            else:
                left = mid + 1

        return left
        # return bisect_left(range(0, n + m + 1), True, key=is_possible)

21400