Python | SortedList | O(nlogn)
Code\n\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        nums.reverse()\n        arr1
# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        nums.reverse()\n        arr1 = [nums.pop()]\n        arr2 = [nums.pop()]\n        sl1 = SortedList(arr1)\n        sl2 = SortedList(arr2)\n        def greater(sl, num):\n            idx = sl.bisect_right(num)\n            return len(sl) - idx\n        while nums:\n            num = nums.pop()\n            if greater(sl1, num) > greater(sl2, num):\n                arr1.append(num)\n                sl1.add(num)\n            elif greater(sl1, num) < greater(sl2, num):\n                arr2.append(num)\n                sl2.add(num)\n            elif len(arr1) < len(arr2):\n                arr1.append(num)\n                sl1.add(num)\n            elif len(arr1) > len(arr2):\n                arr2.append(num)\n                sl2.add(num)\n            else:\n                arr1.append(num)\n                sl1.add(num)\n        return arr1 + arr2\n        \n\n        \n```
['Python3']