It's getting trivial with pbds
\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n\n    using namespace __gnu_pbds;\n    using ordered_set = tree<int, null_type, 
```\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n\n    using namespace __gnu_pbds;\n    using ordered_set = tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>;\n\n    vector<int> resultArray(vector<int>& nums) {\n        vector<int> x = {nums[0]}, y = {nums[1]};\n        ordered_set a, b;\n        a.insert(nums[0]);\n        b.insert(nums[1]);\n\n        for (int i = 2; i < size(nums); ++i) {\n            const auto c1 = a.size() - a.order_of_key(nums[i] + 1);\n            const auto c2 = b.size() - b.order_of_key(nums[i] + 1);\n            if (c1 > c2) {\n                a.insert(nums[i]);\n                x.push_back(nums[i]);\n            } else if (c1 < c2) {\n                b.insert(nums[i]);\n                y.push_back(nums[i]);\n            } else {\n                if (a.size() <= b.size()) {\n                    a.insert(nums[i]);\n                    x.push_back(nums[i]);\n                } else {\n                    b.insert(nums[i]);\n                    y.push_back(nums[i]);\n                }\n            }\n        }\n\n        for (const auto v: y) {\n            x.push_back(v);\n        }\n        return x;\n    }\n```
['C++']