Formatted question description: https://leetcode.ca/all/1775.html

1775. Equal Sum Arrays With Minimum Number of Operations

Level

Medium

Description

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

Use greedy approach. First calculate the sum of nums1 and the sum of nums2 and determine whether it is possible to make two arrays have equal sum. Next count the occurrences of numbers from 1 to 6 in both arrays nums1 and nums2, and count the differences from 1 to 5. Use the difference between two sums and the differences to determine the minimum number of operations.

class Solution {
    public int minOperations(int[] nums1, int[] nums2) {
        int length1 = nums1.length, length2 = nums2.length;
        int minSum1 = length1, maxSum1 = 6 * length1, minSum2 = length2, maxSum2 = 6 * length2;
        if (minSum1 > maxSum2 || maxSum1 < minSum2)
            return -1;
        int sum1 = 0, sum2 = 0;
        int[] counts1 = new int[7];
        int[] counts2 = new int[7];
        for (int i = 0; i < length1; i++) {
            sum1 += nums1[i];
            counts1[nums1[i]]++;
        }
        for (int i = 0; i < length2; i++) {
            sum2 += nums2[i];
            counts2[nums2[i]]++;
        }
        if (sum1 == sum2)
            return 0;
        else if (sum1 < sum2)
            return getOperations(counts1, counts2, sum2 - sum1);
        else
            return getOperations(counts2, counts1, sum1 - sum2);
    }

    public int getOperations(int[] minCounts, int[] maxCounts, int difference) {
        int[] differences = new int[6];
        for (int i = 1; i < 6; i++)
            differences[6 - i] += minCounts[i];
        for (int j = 6; j > 1; j--)
            differences[j - 1] += maxCounts[j];
        int operations = 0;
        for (int i = 5; i > 0 && difference > 0; i--) {
            int count = differences[i];
            int maxReduce = i * count;
            if (maxReduce < difference) {
                difference -= maxReduce;
                operations += count;
            } else {
                int curOperations = difference / i;
                if (difference % i != 0)
                    curOperations++;
                operations += curOperations;
                difference = 0;
            }
        }
        return operations;
    }
}

All Problems

All Solutions