My issue
Debug the above code
My code
#include <bits/stdc++.h>
using namespace std;
int Merge(int arr[],int low,int mid,int high)
{
int left=low,right=mid+1;
int k=0;int cnt=0;
vector<int> temp(high-low+1);
while(left<=mid && right<=high)
{
if(arr[left]<=arr[right])
{
temp[k++]=arr[left++];
}
else
{
cnt+=(mid-left+1);
temp[k++]=arr[right++];
}
}
while(left<=mid)
{
temp[k++]=arr[left++];
}
while(right<=high)
{
temp[k++]=arr[right++];
}
for(int j=low;j<=high;j++)
{
arr[j]=temp[j-low];
}
return cnt;
}
int MergeSort(int a[],int low,int high)
{
int cnt=0;
if(low>=high) return cnt;
int mid=(low+high)/2;
cnt+=MergeSort(a,low,mid);
cnt+=MergeSort(a,mid+1,high);
cnt+=Merge(a,low,mid,high);
return cnt;
}
int main() {
int T;
cin>>T;
while(T--)
{
int N;
cin>>N;
int a[N];
for(int i=0;i<N;i++){
cin>>a[i];
}
int cnt=MergeSort(a,0,N-1);
cout<<cnt<<"\n";
}
}
Learning course: Analysis and Design of Algorithms
Problem Link: Inversion Count in Analysis and Design of Algorithms