Java Solution with comments | Easy to Understand
Intuition\n  Basic need here is to find the greaterCount in an optimized way.\n    So, we made two more sorted lists. In these sorted list, while inserting we w
# Intuition\n  Basic need here is to find the greaterCount in an optimized way.\n    So, we made two more sorted lists. In these sorted list, while inserting we will insert with binary search. Similarly, while finding greaterCount, we will use binary search.\n    Code is explained with comments......So Let\'s Go...\n\n# Code\n```\n\nclass Solution {\n    public int[] resultArray(int[] nums) {\n        int n=nums.length;\n        \n        // two normal lists\n        List<Integer> list1=new ArrayList<>();\n        List<Integer> list2=new ArrayList<>();\n        \n        // two sorted lists\n        List<Integer> sortedList1=new ArrayList<>();\n        List<Integer> sortedList2=new ArrayList<>();\n        \n        \n        //insert in two normal lists\n        list1.add(nums[0]);\n        list2.add(nums[1]);\n        \n        // insert in two sorted lists\n        sortedList1.add(nums[0]);\n        sortedList2.add(nums[1]);\n        \n        for(int i=2;i<n;i++){\n            int greaterCount1=greaterCount(sortedList1,nums[i]);\n            int greaterCount2=greaterCount(sortedList2,nums[i]);\n            \n            // conditions given in problem\n            if(greaterCount1 > greaterCount2)\n                insert(sortedList1,nums[i],list1);\n            else if(greaterCount1 < greaterCount2)\n                insert(sortedList2,nums[i],list2);\n            else if(list1.size() < list2.size())\n                insert(sortedList1,nums[i],list1);\n            else if(list2.size() < list1.size())\n                insert(sortedList2,nums[i],list2);\n            else\n                insert(sortedList1,nums[i],list1);\n            \n        }\n        \n        // concatenate lists to list1\n        list1.addAll(list2);\n        \n        //convert list to array\n        int[] ans=new int[n];\n        for(int i=0;i<n;i++) ans[i]=list1.get(i);\n        return ans;\n    }\n    \n    \n    public void insert(List<Integer> sortedList, int element, List<Integer> list) {\n        // insert at last index of a normal list\n        list.add(element);\n        \n        // insert element in a sorted list    \n        int index = Collections.binarySearch(sortedList, element);\n        if (index < 0) {\n            index = -index - 1;\n        }\n        sortedList.add(index, element);\n    }\n    \n    \n    // function to find count of greater elements than val\n    public int greaterCount(List<Integer> list,int val){\n        int n=list.size();\n        int l = 0;\n        int r = n - 1;\n        int leftGreater = n;\n        while (l <= r) {\n            int m = l + (r - l) / 2;\n            if (list.get(m) > val) {\n                leftGreater = m;\n                r = m - 1;\n            }\n            else l = m + 1;\n        }\n        return n - leftGreater;\n    }\n    \n}\n```
['Java']