Policy Based Data Structure | Distribute elements | C++ | Python | Above 90% Beat TC
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n  O(N * log(N))\n\n- Space complexity:\n  O(N)\n\n# Code\n```cpp []\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing ordered_multiset = tree<int, null_type,\n                              less_equal<int>, rb_tree_tag,\n                              tree_order_statistics_node_update>;\n\nclass Solution {\npublic:\n\n    int greaterCount(ordered_multiset &set, int num){\n          //num+1 is used so that it also consider dublicates\n          int total_smaller_elements = set.order_of_key(num+1);\n          int total_elements = set.size();\n          return total_elements - total_smaller_elements;\n    }\n    vector<int> resultArray(vector<int>& nums) {\n        vector<int>arr1 = {nums[0]};\n        vector<int>arr2 = {nums[1]};\n        ordered_multiset set1, set2;\n        set1.insert(nums[0]);\n        set2.insert(nums[1]);\n        int countArr1 = 0;\n        int countArr2 = 0;\n        int i = 2;\n        while (i < nums.size()){\n           countArr1 = greaterCount(set1, nums[i]);\n           countArr2 = greaterCount(set2, nums[i]);\n\n            if (countArr1 > countArr2){\n                arr1.push_back(nums[i]);\n                set1.insert(nums[i]);\n            }\n            else if (countArr1 < countArr2){\n                arr2.push_back(nums[i]);\n                set2.insert(nums[i]);\n            }\n            else{\n                if(arr2.size() < arr1.size()){\n                    arr2.push_back(nums[i]);\n                    set2.insert(nums[i]);\n                }\n                else{\n                    arr1.push_back(nums[i]);\n                    set1.insert(nums[i]);\n                }\n            }\n            i++;\n        }\n        vector<int> result(arr1);\n        result.insert(result.end(), arr2.begin(), arr2.end());\n        return result;\n    }\n};\n```\n```python []\nfrom sortedcontainers import SortedList\n\nclass Solution(object):\n\n    def greaterCount(self, st,  num):\n        total_smaller_elements = st.bisect_left(num+1)\n        total_elements = len(st)\n        return total_elements - total_smaller_elements\n\n    def resultArray(self, nums):\n        self.arr1 = [nums[0]]\n        self.arr2 = [nums[1]]\n        self.set1, self.set2 = SortedList([nums[0]]), SortedList([nums[1]])\n        self.countArr1 = 0\n        self.countArr2 = 0\n        self.i = 2\n        while self.i < len(nums):\n            \n            self.countArr1 = self.greaterCount(self.set1, nums[self.i])\n            self.countArr2 = self.greaterCount(self.set2, nums[self.i])\n\n            if self.countArr1 >  self.countArr2:\n                self.arr1.append(nums[self.i])\n                self.set1.add(nums[self.i])\n            elif self.countArr1 <  self.countArr2:\n                self.arr2.append(nums[self.i])\n                self.set2.add(nums[self.i])\n            else:\n                if len(self.arr2) < len(self.arr1):\n                    self.arr2.append(nums[self.i])\n                    self.set2.add(nums[self.i])\n                else:\n                    self.arr1.append(nums[self.i])\n                    self.set1.add(nums[self.i])\n            self.i+=1\n        return self.arr1 + self.arr2\n                                \n\n
['C++']