a few solutions
Game Plan\nSince N = 100,000 we need to use logarithmic lookups to avoid TLE, ie. use a sorted set for each partition A and B of the input numbers nums to perfo
# Game Plan\nSince `N = 100,000` we need to use logarithmic lookups to avoid TLE, ie. use a sorted set for each partition `A` and `B` of the input numbers `nums` to perform binary search to find the quantity of "greater-than" `x` values.\n\n# Code\n\n*Python3: AC*\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        A = SortedList([nums[0]]); A_list = [nums[0]]\n        B = SortedList([nums[1]]); B_list = [nums[1]]\n        for x in nums[2:]:\n            a = len(A) - A.bisect_right(x)\n            b = len(B) - B.bisect_right(x)\n            if a > b: A.add(x); A_list.append(x)\n            if a < b: B.add(x); B_list.append(x)\n            if a == b:\n                if len(A) <= len(B):\n                    A.add(x); A_list.append(x)\n                else:\n                    B.add(x); B_list.append(x)\n        return A_list + B_list\n```\n\n*C++: TLE \uD83D\uDCA9*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using Set = multiset<int>;\n    VI resultArray(VI& nums) {\n        Set A{ nums[0] }; VI A_list{ nums[0] };\n        Set B{ nums[1] }; VI B_list{ nums[1] };\n        for (auto i{ 2 }; i < nums.size(); ++i) {\n            auto x = nums[i];\n            auto a = A.size() - distance(A.begin(), A.upper_bound(x));\n            auto b = B.size() - distance(B.begin(), B.upper_bound(x));\n            if (a > b) A.insert(x), A_list.push_back(x);\n            if (a < b) B.insert(x), B_list.push_back(x);\n            if (a == b) {\n                if (A.size() <= B.size())\n                    A.insert(x), A_list.push_back(x);\n                else\n                    B.insert(x), B_list.push_back(x);\n            }\n        }\n        A_list.insert(A_list.end(), B_list.begin(), B_list.end());\n        return A_list;\n    }\n};\n```\n\n*C++: TLE still \uD83D\uDC7B*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using Set = multiset<int>;\n    VI resultArray(VI& nums) {\n        Set A{ nums[0] }; VI A_list{ nums[0] };\n        Set B{ nums[1] }; VI B_list{ nums[1] };\n        for (auto i{ 2 }; i < nums.size(); ++i) {\n            auto x = nums[i];\n            auto it_a = A.upper_bound(x),\n                 it_b = B.upper_bound(x);\n            auto a = A.size() - distance(A.begin(), it_a);\n            auto b = B.size() - distance(B.begin(), it_b);\n            if (a > b) A.insert(it_a, x), A_list.push_back(x);\n            if (a < b) B.insert(it_b, x), B_list.push_back(x);\n            if (a == b) {\n                if (A.size() <= B.size())\n                    A.insert(it_a, x), A_list.push_back(x);\n                else\n                    B.insert(it_b, x), B_list.push_back(x);\n            }\n        }\n        A_list.insert(A_list.end(), B_list.begin(), B_list.end());\n        return A_list;\n    }\n};\n```
['C++', 'Python3']