<p>Given an integer array <code>nums</code>, return<em> an integer array </em><code>counts</code><em> where </em><code>counts[i]</code><em> is the number of smaller elements to the right of </em><code>nums[i]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,6,1]
<strong>Output:</strong> [2,1,1,0]
<strong>Explanation:</strong>
To the right of 5 there are <b>2</b> smaller elements (2 and 1).
To the right of 2 there is only <b>1</b> smaller element (1).
To the right of 6 there is <b>1</b> smaller element (1).
To the right of 1 there is <b>0</b> smaller element.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1]
<strong>Output:</strong> [0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-1]
<strong>Output:</strong> [0,0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

// #include <ext/pb_ds/assoc_container.hpp>
// // #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

// class Solution {
// public:
//     vector<int> countSmaller(vector<int>& nums) {
//         oset<int> st;
//         vector<int> ans;
//         int n = nums.size();
//         for(int i=n-1; i>=0; --i){
//             cout << st.order_of_key(nums[i]) << " ";
//             ans.push_back(st.order_of_key(nums[i]));
//             st.insert(nums[i]);
//         }
//         reverse(ans.begin(), ans.end());
//         return ans;
//     }
// };

// interview me merge sort se krna
class Solution {
public:
    void merge(vector<int> &count, vector<pair<int, int>> &v, int l, int m, int r){
    int n1 = m - l + 1, n2 = r - m;
    vector<pair<int,int>> left(n1), right(n2);
    for (int i = 0; i < n1; i++)
        left[i] = v[i+l];
    for (int j = 0; j < n2; j++)
        right[j] = v[m+1+j];

    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {

        if (left[i].first <= right[j].first) {
            v[k++] = right[j++];
            
        }
        else {
            count[left[i].second]+=r-j-m;
            v[k++] = left[i++];  
        }
    }
    while (i < n1)
        v[k++] = left[i++];
        
    while (j < n2)
        v[k++] = right[j++];  
    }
    void mergeSort(vector<int> &count, vector<pair<int,int>> &v, int left, int right){
        if(left<right){
            int mid = left + (right-left)/2;
            mergeSort(count, v, left, mid);
            mergeSort(count, v, mid+1, right);
            merge(count, v, left, mid, right); // here merge array in descending order
        }
    }
    vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        vector<pair<int,int>> v(n);
        for(int i=0;i<n;i++){
            v[i]={nums[i],i};
        }
        vector<int> count(n, 0);
        mergeSort(count, v, 0, n-1);
        return count;
    }
};
300731