What is wrong with this solution(INVERSIONS)?


#1

int merge(vector a,int l,int m,int r)
{
int i=l,j=m+1,c=0,cnt=0;
int b[r-l+10];
while(i<=m && j<=r)
{
if(a[i]<=a[j])
b[c++]=a[i++];
else
{
cnt+=(m-i+1);
b[c++]=a[j++];
}
}
while(i<=m)
b[c++]=a[i++];
while(j<=r)
b[c++]=a[j++];
for(i=0;i<c;i++)
{
a[i+l]=b[i];
}
return cnt;
}

int mergesort(vector A,int l,int r)
{
int cnt=0;
if(l<r)
{
int mid=(l+r)/2;
cnt=mergesort(A,l,mid);
cnt+=mergesort(A,mid+1,l);
cnt+=merge(A,l,mid,r);
}
return cnt;
}

int Solution::countInversions(vector &A) {
int n=A.size();
return mergesort(A,0,(int)n-1);
}


#2

in your merge function, after the first while loop to increment the count, u have to reset the values of i and j also