LeetCode in Kotlin

2602. Minimum Operations to Make All Array Elements Equal

Medium

You are given an array nums consisting of positive integers.

You are also given an integer array queries of size m. For the ith query, you want to make all of the elements of nums equal to queries[i]. You can perform the following operation on the array any number of times:

Return an array answer of size m where answer[i] is the minimum number of operations to make all elements of nums equal to queries[i].

Note that after each query the array is reset to its original state.

Example 1:

Input: nums = [3,1,6,8], queries = [1,5]

Output: [14,10]

Explanation: For the first query we can do the following operations:

So the total number of operations for the first query is 2 + 5 + 7 = 14.

For the second query we can do the following operations:

So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.

Example 2:

Input: nums = [2,9,6,3], queries = [10]

Output: [20]

Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.

Constraints:

Solution

class Solution {
    fun minOperations(nums: IntArray, queries: IntArray): List<Long> {
        nums.sort()
        val sum = LongArray(nums.size)
        sum[0] = nums[0].toLong()
        for (i in 1 until nums.size) {
            sum[i] = sum[i - 1] + nums[i].toLong()
        }
        val res: MutableList<Long> = ArrayList()
        for (query in queries) {
            res.add(getOperations(sum, nums, query))
        }
        return res
    }

    private fun getOperations(sum: LongArray, nums: IntArray, target: Int): Long {
        var res: Long = 0
        val index = getIndex(nums, target)
        val rightCounts = nums.size - 1 - index
        if (index > 0) {
            res += index.toLong() * target - sum[index - 1]
        }
        if (rightCounts > 0) {
            res += sum[nums.size - 1] - sum[index] - rightCounts.toLong() * target
        }
        res += kotlin.math.abs(target - nums[index]).toLong()
        return res
    }

    private fun getIndex(nums: IntArray, target: Int): Int {
        var index = nums.binarySearch(target)
        if (index < 0) index = -(index + 1)
        if (index == nums.size) --index
        return index
    }
}