3072. Distribute Elements Into Two Arrays II
You are given a 1-indexed array of integers
numsof lengthn.We define a function
greaterCountsuch thatgreaterCount(arr, val)returns the number of elements inarrthat are strictly greater thanval.You need to distribute all the elements of
numsbetween two arraysarr1andarr2usingnoperations. In the first operation, appendnums[1]toarr1. In the second operation, appendnums[2]toarr2. Afterwards, in theithoperation:
- If
greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), appendnums[i]toarr1.- If
greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), appendnums[i]toarr2.- If
greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), appendnums[i]to the array with a lesser number of elements.- If there is still a tie, append
nums[i]toarr1.The array
resultis formed by concatenating the arraysarr1andarr2. For example, ifarr1 == [1,2,3]andarr2 == [4,5,6], thenresult = [1,2,3,4,5,6].Return the integer array
result.