LeetCode in Kotlin

954. Array of Doubled Pairs

Medium

Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.

Example 1:

Input: arr = [3,1,3,6]

Output: false

Example 2:

Input: arr = [2,1,2,6]

Output: false

Example 3:

Input: arr = [4,-2,2,-4]

Output: true

Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Constraints:

Solution

class Solution {
    fun canReorderDoubled(arr: IntArray): Boolean {
        val max = 0.coerceAtLeast(arr.max())
        val min = 0.coerceAtMost(arr.min())
        val positive = IntArray(max + 1)
        val negative = IntArray(-min + 1)
        for (a in arr) {
            if (a < 0) {
                negative[-a]++
            } else {
                positive[a]++
            }
        }
        return if (positive[0] % 2 != 0) {
            false
        } else {
            validateFrequencies(positive, max) && validateFrequencies(negative, -min)
        }
    }

    private fun validateFrequencies(frequencies: IntArray, limit: Int): Boolean {
        for (i in 0..limit) {
            if (frequencies[i] == 0) {
                continue
            }
            if (2 * i > limit || frequencies[2 * i] < frequencies[i]) {
                return false
            }
            frequencies[2 * i] -= frequencies[i]
        }
        return true
    }
}