Getting wrong ans


#1

int Merge(vector v, int start1, int start2, int end){
int s1 = start1, e1 = start2-1, s2 = start2, e2 = end;
int cnt = 0;
vector temp(v.size());
int k = 0;
while(s1 <= e1 && s2 <= e2){
if(v[s1] <= v[s2]){
temp[k++] = v[s1++];
}else{
cnt += (start2-s1);
temp[k++] = v[s2++];
}
}
while(s1 <= e1){
temp[k++] = v[s1++];
}
while(s2 <= e2){
temp[k++] = v[s2++];
}
for(int i = start1; i <= end; i++){
v[i] = temp[i];
}
return cnt;
}

int MergeSort(vector v, int start, int end){
if(start >= end){
return 0;
}
int inv_cnt = 0;
int mid = (start + end)/2;
inv_cnt += MergeSort(v, start, mid);
inv_cnt += MergeSort(v, mid+1, end);
inv_cnt += Merge(v, start, mid+1, end);
return inv_cnt;
}

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