You are given a 1-indexed array of integers nums of length n.
We define a function greaterCount such that greaterCount(arr, val) returns the number of elements in arr that are strictly greater than val.
You need to distribute all the elements of nums between two arrays arr1
and arr2 using n operations. In the first operation, append nums[1] to
arr1. In the second operation, append nums[2] to arr2. Afterwards, in the ith operation:
If greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), append nums[i] to arr1.
If greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), append nums[i] to arr2.
If greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), append nums[i] to the array with a lesser number of elements.
If there is still a tie, append nums[i] to arr1.
The array result is formed by concatenating the arrays arr1 and arr2.
For example, if arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].
Input: nums =[2,1,3,3]Output: [2,3,1,3]Explanation: After the first 2 operations, arr1 =[2] and arr2 =[1].In the 3rd operation, the number of elements greater than 3is zero in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.In the 4th operation, the number of elements greater than 3is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.After 4 operations, arr1 =[2,3] and arr2 =[1,3].Hence, the array result formed by concatenation is[2,3,1,3].
Input: nums =[5,14,3,1,2]Output: [5,3,1,2,14]Explanation: After the first 2 operations, arr1 =[5] and arr2 =[14].In the 3rd operation, the number of elements greater than 3is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.In the 4th operation, the number of elements greater than 1is greater in arr1 than arr2(2>1). Hence, append nums[4] to arr1.In the 5th operation, the number of elements greater than 2is greater in arr1 than arr2(2>1). Hence, append nums[5] to arr1.After 5 operations, arr1 =[5,3,1,2] and arr2 =[14].Hence, the array result formed by concatenation is[5,3,1,2,14].
Input: nums =[3,3,3,3]Output: [3,3,3,3]Explanation: At the end of 4 operations, arr1 =[3,3] and arr2 =[3,3].Hence, the array result formed by concatenation is[3,3,3,3].
To efficiently count the number of elements greater than a value in each array, we can use a Binary Indexed Tree (Fenwick Tree) to maintain a frequency table of inserted values. Since values can be up to 1e9, we compress the values to a smaller range using coordinate compression. For each insertion, we can query the number of elements greater than a value in O(log n) time.
classBIT(val n: Int) {
val t = IntArray(n + 2)
funadd(i0: Int, v: Int) {
var i = i0
while (i <= n) {
t[i] += v
i += i and -i
}
}
funsum(i0: Int): Int {
var i = i0
var s = 0while (i > 0) {
s += t[i]
i -= i and -i
}
return s
}
funrange(l: Int, r: Int) = sum(r) - sum(l - 1)
}
classSolution {
funresultArray(nums: IntArray): IntArray {
val n = nums.size
val sorted = nums.sorted().distinct()
val idx = sorted.withIndex().associate { it.value to it.index + 1 }
val bit1 = BIT(n)
val bit2 = BIT(n)
val arr1 = mutableListOf(nums[0])
val arr2 = mutableListOf(nums[1])
bit1.add(idx[nums[0]]!!, 1)
bit2.add(idx[nums[1]]!!, 1)
for (i in2 until n) {
val id = idx[nums[i]]!!val g1 = bit1.range(id + 1, n)
val g2 = bit2.range(id + 1, n)
if (g1 > g2) {
arr1.add(nums[i])
bit1.add(id, 1)
} elseif (g1 < g2) {
arr2.add(nums[i])
bit2.add(id, 1)
} else {
if (arr1.size <= arr2.size) {
arr1.add(nums[i])
bit1.add(id, 1)
} else {
arr2.add(nums[i])
bit2.add(id, 1)
}
}
}
return (arr1 + arr2).toIntArray()
}
}