LeetCode in Kotlin

2541. Minimum Operations to Make Array Equal II

Medium

You are given two integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1:

nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < n, nums1[i] == nums2[i].

Return the minimum number of operations required to make nums1 equal to nums2. If it is impossible to make them equal, return -1.

Example 1:

Input: nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3

Output: 2

Explanation: In 2 operations, we can transform nums1 to nums2.

1st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].

2nd operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1]. One can prove that it is impossible to make arrays equal in fewer operations.

Example 2:

Input: nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1

Output: -1

Explanation: It can be proved that it is impossible to make the two arrays equal.

Constraints:

Solution

class Solution {
    fun minOperations(nums1: IntArray, nums2: IntArray, k: Int): Long {
        val n = nums1.size
        var pcnt: Long = 0
        var ncnt: Long = 0
        if (k == 0) {
            return if (nums1.contentEquals(nums2)) {
                0
            } else {
                -1
            }
        }
        for (i in 0 until n) {
            val tp = nums1[i] - nums2[i]
            if (tp % k != 0) {
                return -1
            }
            if (tp > 0) {
                pcnt += tp.toLong()
            } else if (tp < 0) {
                ncnt += tp.toLong()
            }
        }
        return if (pcnt + ncnt != 0L) {
            -1
        } else {
            pcnt / k
        }
    }
}