Binary Search || Detailed DryRun Explanation
ApproachThe code is very much straightforward but there is one thing that might be confusing, which is the calculation of count1 and count2.In this code, we are
# Approach
<!-- Describe your approach to solving the problem. --> 

The code is very much straightforward but there is one thing that might be confusing, which is the calculation of count1 and count2.  

In this code, we are assigning the 0th element and the 1st element of the array, into the ARR1 and ARR2. 

And we are also using two other array is called as sorted1 and sorted2.
These two arrays will be used for binary search, because in these two, we will be placing the elements into the sorted order and the elements which will be in ARR1 will be sorted1, in the elements which will be in ARR2 will be in to sorted2.

Let's see how we are finding the account here, which is the most important part, because, based on that only we will be pushing the elements into the two respective arrays, 
And after that, we will just insert the values of arr2 into the ARR1, and that will be the answer

----

    int count1 = sorted1.size() - (upper_bound(sorted1.begin(), 
    sorted1.end(), nums[i]) - sorted1.begin());

1. upper_bound(sorted1.begin(), sorted1.end(), nums[i]) - This function performs a binary search on the sorted array sorted1 and returns an iterator pointing to the first element that is greater than nums[i].

2. upper_bound(sorted1.begin(), sorted1.end(), nums[i]) - sorted1.begin() - This subtracts the beginning iterator of the array from the result of upper_bound. This gives us the index or position where the first element greater than nums[i] appears in the sorted array.

3 . sorted1.size() - (upper_bound(...)) - This calculates how many elements are greater than nums[i] in the array: sorted1.size() is the total number of elements in the array
The result of step 2 tells us how many elements are less than or equal to nums[i]

So subtracting this from the total size gives us the count of elements that are greater than nums[i].

----

## Example

#### sorted1 = [1, 3, 5, 7, 9] and nums[i] = 4
#### upper_bound(sorted1.begin(), sorted1.end(), 4) would point to the element 5 (at index 2)
#### upper_bound(...) - sorted1.begin() equals 2
#### sorted1.size() - 2 equals 5 - 2 = 3 
### So there are 3 elements (5, 7, and 9) that are greater than 4
----




# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

    O(N log N)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(N)

# Code
```cpp []
class Solution {
public:
    vector<int> resultArray(vector<int>& nums) {
        if (nums.size() <= 2) {
            return nums;
        }
        
        vector<int> arr1 = {nums[0]};
        vector<int> arr2 = {nums[1]};

        vector<int> sorted1 = {nums[0]};
        vector<int> sorted2 = {nums[1]};
        
        for (int i = 2; i < nums.size(); i++) {
            // Calculate greaterCount for both arrays using binary search
            int count1 = sorted1.size() - (upper_bound(sorted1.begin(), sorted1.end(), nums[i]) - sorted1.begin());

            int count2 = sorted2.size() - (upper_bound(sorted2.begin(), sorted2.end(), nums[i]) - sorted2.begin());
            
            if (count1 > count2) {
                arr1.push_back(nums[i]);
                auto pos = upper_bound(sorted1.begin(), sorted1.end(), nums[i]);
                sorted1.insert(pos, nums[i]);
            } else if (count2 > count1) {
                arr2.push_back(nums[i]);
                auto pos = upper_bound(sorted2.begin(), sorted2.end(), nums[i]);
                sorted2.insert(pos, nums[i]);
            } else {
                if (arr1.size() <= arr2.size()) {
                    arr1.push_back(nums[i]);
                    auto pos = upper_bound(sorted1.begin(), sorted1.end(), nums[i]);
                    sorted1.insert(pos, nums[i]);
                } else {
                    arr2.push_back(nums[i]);
                    auto pos = upper_bound(sorted2.begin(), sorted2.end(), nums[i]);
                    sorted2.insert(pos, nums[i]);
                }
            }
        }
        
        arr1.insert(arr1.end(), arr2.begin(), arr2.end());
        return arr1;
    }
};
```
['Array', 'Binary Indexed Tree', 'Segment Tree', 'Simulation', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'Python ML']