7. 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].

Solution: (Greedy)

1.In order to make the two sum's equal, we need either to increase to 6 the numbers in the smaller sum array or decrease to 1 the numbers in the bigger sum array

2. Since we want to complete the task with minimum operations, it is optimal to choose the greater between the increase and decrease. Hence this is a greedy algorithm.

Note: If all numbers in an array increasing to 6 still ends up with a sum less than the sum of the other array with all numbers decreasing to 1's, then it is impossible to make their sums equal.

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

        sort(nums1.begin(), nums1.end(), greater<int>());
        sort(nums2.begin(), nums2.end());

        int s1 = 0, s2 = 0;
        for (int i = 0; i < nums1.size(); i++)
        {
            s1 += nums1[i];
        }

        for (int i = 0; i < nums2.size(); i++)
        {
            s2 += nums2[i];
        }

        if (s2 > s1)
        {
            return minOperations(nums2, nums1);
        }

        if (nums1.size() > 6 * nums2.size())
        {
            return -1;
        }

        int i = 0;
        int j = 0;
        int ops = 0;

        while (s1 > s2)
        {

            if (i < nums1.size() && j < nums2.size())
            {
                int d1 = nums1[i] - 1;
                int d2 = 6 - nums2[j];

                if (d1 > d2)
                {
                    s1 = s1 - d1;
                    i++;
                }
                else
                {
                    s2 = s2 + d2;
                    j++;
                }

                ops++;
            }
            else
            {
                break;
            }
        }

        while (s1 > s2)
        {
            if (i < nums1.size())
            {
                s1 = s1 - (nums1[i] - 1);
                i++;
                ops++;
            }
        }

        while (s1 > s2)
        {
            if (j < nums2.size())
            {
                s2 = s2 + (6 - nums2[j]);
                j++;
                ops++;
            }
        }
        return ops;
    }
};

Time Complexity: O max(n + m)

Last updated