<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>

class Solution {
public:
//https://leetcode.com/problems/count-of-smaller-numbers-after-self/solutions/76607/c-o-nlogn-time-o-n-space-mergesort-solution-with-detail-explanation/
void print(vector<int>& v)
{
int n=v.size();
for (int i=0;i<n;i++)
    cout<<v[i]<<" ";
cout<<endl;
}
    void support(vector<int>& indices, int start, int end,vector<int>& results, vector<int>& nums )
    {
        int count = end - start;
      //  cout<<" start="<<start<<" end="<<end<<endl;
        if(count>1)
        {
            int mid = start + (end-start)/2;
            support(indices,start,mid,results,nums);
            support(indices,mid,end,results,nums);
            vector<int> tmp;//(count);
            tmp.reserve(count);
            int idx1=start;
            int idx2=mid;
            int sizeMergedArray2=0;
            //cout<<"inner start="<<start<<" mid="<<mid<<" end="<<end<<endl;
            while((idx1<mid || idx2<end))
            {   //take from first gropu only if either second is processed, 
                // or first satill has elements, and also inequilaity holds
                //cout<<"idx1="<<idx1<<" idx2="<<idx2<<endl;
              
                if(( idx2==end) ||(idx1<mid && nums[indices[idx1]]<= nums[indices[idx2]]))
                {//take from first list
                  //cout<<" indices[idx1]="<<indices[idx1]<<" indices[idx2]="<<indices[idx2]<<endl;
                  //cout<<" take left index "<<indices[idx1]<<" which has value "<<nums[indices[idx1]]<<endl;
                 tmp.push_back(indices[idx1]);
                 results[indices[idx1]]+=sizeMergedArray2;
                 //cout<<" add to results["<<indices[idx1]<<"] value "<<sizeMergedArray2<<endl;
                 idx1++;
                }
                else
                {//take from second list
                //cout<<" indices[idx1]="<<indices[idx1]<<" indices[idx2]="<<indices[idx2]<<endl;
                //cout<<" take right index "<<indices[idx2]<<" which has value "<<nums[indices[idx2]]<<endl;
                tmp.push_back(indices[idx2]);
                sizeMergedArray2++;
                idx2++;
                }
            }
            std::move(tmp.begin(),tmp.end(),indices.begin()+start);
           // cout<<"print indices"<<endl;
           // print(indices);
        }
    }
    //nums [5,2,6,1]
    //indices [0,1,2,3]
    // on interval [0,2] indices=> [1,0, and unchanged 2,3]
    // on interval [2,4] indices=> [unchanged 1,0, and 3,2]
    //then we have 2 ranges [1,0] and [3,2] of indices (last merge of 2 subaaray of size 2 and 2)
    //lets join them nums[1] = 2 > nums[3]=1
    //so take 3 ,=> new_indices= [3]
    //nums[1] = 2 < nums[2]= 6 , so take 1 , new_indices=[3,1]
    //nums[0]=5 < nums[2]=6 , so take 0 ,new_indices=[3,1,0]
    //left with index 2, =>new_indices=[3,1,0,2] 
    vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        vector<int> indices(n,0);//will store indices we process
        vector<int> results(n,0);
        iota(indices.begin(),indices.end(),0);
        support(indices,0,n,results, nums);
        return results;
    }
};
189693