c++ solution || using binary search || easy to understand || ✅✅🔥
\n# Code\n\nclass Solution {\npublic:\n    vector<int> resultArray(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> a1, a2, l, r;\n      
\n# Code\n```\nclass Solution {\npublic:\n    vector<int> resultArray(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> a1, a2, l, r;\n        a1.push_back(nums[0]);\n        a2.push_back(nums[1]);\n        l.push_back(nums[0]);\n        r.push_back(nums[1]);\n        for(int i = 2; i < n; i++){\n            int x = a1.end()-upper_bound(a1.begin(), a1.end(), nums[i]);\n            int y = a2.end()-upper_bound(a2.begin(), a2.end(), nums[i]);\n            if(x>y){\n                l.push_back(nums[i]);\n                a1.insert(a1.end()-x, nums[i]);\n            }\n            else if(x<y){\n                r.push_back(nums[i]);\n                a2.insert(a2.end()-y, nums[i]);\n            }\n            else{\n                if(a1.size()<=a2.size()){\n                    l.push_back(nums[i]);\n                    a1.insert(a1.end()-x, nums[i]);\n                }\n                else{\n                    r.push_back(nums[i]);\n                    a2.insert(a2.end()-y, nums[i]);  \n                }\n            }\n        }\n        vector<int> res(l.begin(), l.end());\n        for(int i =0; i < r.size(); i++){\n            res.push_back(r[i]);\n        }\n        return res;\n    }\n};\n```
['Binary Search', 'C++']