With the help of PBDS
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<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n\ntypedef tree<\npair<int, int>,\nnull_type,\nless<pair<int, int>>,\nrb_tree_tag,\ntree_order_statistics_node_update>\nordered_set;\nclass Solution {\npublic:\n    vector<int> resultArray(vector<int>& nums) {\n        ordered_set s1, s2 ;\n        int n = nums.size() ;\n        vector<int> arr1, arr2, ans ;\n        s1.insert({nums[0], 0}) ;\n        s2.insert({nums[1], 1}) ;\n        arr1.push_back(nums[0]) ;\n        arr2.push_back(nums[1]) ;\n\n        for (int i = 2; i < n; i++) {\n            int val = nums[i] + 1 ;\n            int ind1 = s1.order_of_key({val, 0}) ;\n            int ind2 = s2.order_of_key({val, 0}) ;\n\n            int sz1 = s1.size() - ind1 ;\n            int sz2 = s2.size() - ind2 ;\n            if (sz1 > sz2) {\n                arr1.push_back(nums[i]);\n                s1.insert({nums[i], i});\n            } else if (sz1 < sz2) {\n                arr2.push_back(nums[i]) ;\n                s2.insert({nums[i], i});\n            } else {\n                if (arr1.size() <= arr2.size()) {\n                    arr1.push_back(nums[i]);\n                    s1.insert({nums[i], i}) ;\n                } else {\n                    arr2.push_back(nums[i]) ;\n                    s2.insert({nums[i], i});\n                }\n            }\n        }\n      \n      ans = arr1;\n      for (auto& it : arr2) {\n        ans.push_back(it) ;\n      }\n\n      return ans;\n        \n    }\n};\n```
['C++']