LeetCode in Kotlin

1775. Equal Sum Arrays With Minimum Number of Operations

Medium

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.

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.

Constraints:

Solution

class Solution {
    fun minOperations(nums1: IntArray, nums2: IntArray): Int {
        val longer = if (nums1.size > nums2.size) nums1 else nums2
        val shorter = if (nums1.size > nums2.size) nums2 else nums1
        if (longer.size > shorter.size * 6) {
            return -1
        }
        longer.sort()
        shorter.sort()
        var i = 0
        var j = 0
        var diff = 0
        while (i < longer.size || j < shorter.size) {
            if (i < longer.size) {
                diff += longer[i++]
            }
            if (j < shorter.size) {
                diff -= shorter[j++]
            }
        }
        var minOps = 0
        i = 0
        j = shorter.size - 1
        return if (diff < 0) {
            while (diff < 0) {
                diff += if (i < longer.size && j >= 0) {
                    if (6 - longer[i] < shorter[j] - 1) {
                        shorter[j--] - 1
                    } else {
                        6 - longer[i++]
                    }
                } else if (i < longer.size) {
                    6 - longer[i++]
                } else {
                    shorter[j--] - 1
                }
                minOps++
            }
            minOps
        } else if (diff > 0) {
            i = longer.size - 1
            j = 0
            while (diff > 0) {
                diff -= if (i >= 0 && j < shorter.size) {
                    if (longer[i] - 1 > 6 - shorter[j]) {
                        longer[i--] - 1
                    } else {
                        6 - shorter[j++]
                    }
                } else if (i >= 0) {
                    longer[i--] - 1
                } else {
                    6 - shorter[j++]
                }
                minOps++
            }
            minOps
        } else {
            minOps
        }
    }
}