LeetCode in Kotlin

2449. Minimum Number of Operations to Make Arrays Similar

Hard

You are given two positive integer arrays nums and target, of the same length.

In one operation, you can choose any two distinct indices i and j where 0 <= i, j < nums.length and:

Two arrays are considered to be similar if the frequency of each element is the same.

Return the minimum number of operations required to make nums similar to target. The test cases are generated such that nums can always be similar to target.

Example 1:

Input: nums = [8,12,6], target = [2,14,10]

Output: 2

Explanation: It is possible to make nums similar to target in two operations:

It can be shown that 2 is the minimum number of operations needed.

Example 2:

Input: nums = [1,2,5], target = [4,1,3]

Output: 1

Explanation: We can make nums similar to target in one operation: - Choose i = 1 and j = 2, nums = [1,4,3].

Example 3:

Input: nums = [1,1,1,1,1], target = [1,1,1,1,1]

Output: 0

Explanation: The array nums is already similiar to target.

Constraints:

Solution

class Solution {
    fun makeSimilar(nums: IntArray, target: IntArray): Long {
        val evenNums = ArrayList<Int>()
        val oddNums = ArrayList<Int>()
        val evenTar = ArrayList<Int>()
        val oddTar = ArrayList<Int>()
        nums.sort()
        target.sort()
        for (i in nums.indices) {
            if (nums[i] % 2 == 0) {
                evenNums.add(nums[i])
            } else {
                oddNums.add(nums[i])
            }
            if (target[i] % 2 == 0) {
                evenTar.add(target[i])
            } else {
                oddTar.add(target[i])
            }
        }
        var countPositiveIteration: Long = 0
        var countNegativeIteration: Long = 0
        for (i in evenNums.indices) {
            val num = evenNums[i]
            val tar = evenTar[i]
            val diff = num.toLong() - tar
            val iteration = diff / 2
            if (diff > 0) {
                countNegativeIteration += iteration
            } else if (diff < 0) {
                countPositiveIteration += Math.abs(iteration)
            }
        }
        for (i in oddNums.indices) {
            val num = oddNums[i]
            val tar = oddTar[i]
            val diff = num.toLong() - tar
            val iteration = diff / 2
            if (diff > 0) {
                countNegativeIteration += iteration
            } else if (diff < 0) {
                countPositiveIteration += Math.abs(iteration)
            }
        }
        val totalDifference = countPositiveIteration - countNegativeIteration
        return if (totalDifference == 0L) countPositiveIteration else countPositiveIteration + Math.abs(totalDifference)
    }
}