# Help with a good problem on sorting

Below is the link to the problem and my code. I could only pass case 1 with my code. And other cases are showing wrong answers. It would be of great help if anyone could identify the errors in my code.

``````#include<bits/stdc++.h>

using namespace std;

vector<long long int>freq(1000000,0);

void merge(vector<long long int>&v,long long int low,long long int mid,long long int high)
{
long long int count=0;
long long int i = low,j = mid+1,k=0;
vector<long long int>temp(high-low+1);
for(long long int p=low;p<=high;p++)
{
if(i>mid)
{
temp[k++] = v[j++];
}
else if(j>high)
{
temp[k++] = v[i];
freq[v[i]]+=count;
i++;
}
else if(v[i]<=v[j])
{
temp[k++] = v[i];
freq[v[i]]+=count;
i++;
}
else
{
temp[k++] = v[j++];
count++;
}
}

for(long long int p = 0;p<k;p++)
{
v[low+p] = temp[p];
}
}

void merge_sort(vector<long long int>&v,long long int low,long long int high)
{
if(low>=high)
{
return;
}
long long int mid = (low+high)/2;
merge_sort(v,low,mid);
merge_sort(v,mid+1,high);
merge(v,low,mid,high);
}

int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int n,x;
cin>>n;
vector<long long int> v;
for(long long int i=0;i<n;i++)
{
cin>>x;
v.push_back(x);
}
vector<long long int>w = v;
merge_sort(v,0,n-1);
for(long long int i=0;i<n;i++)
{
cout<<freq[w[i]]<<" ";
}
cout<<endl;
}
}``````

Your solution is correct . The problem is that when next test case runs the freq vector must be reset to 0 but you are not doing so. To reset it to 0 , you can use fill(freq.begin(),freq.end(),0); in your main function.

Thank you. I appreciate your help.