Getting Wrong answer for testcase


#1

A : [ 84, 2, 37, 3, 67, 82, 19, 97, 91, 63, 27, 6, 13, 90, 63, 89, 100, 60, 47, 96, 54, 26, 64, 50, 71, 16, 6, 40, 84, 93, 67, 85, 16, 22, 60 ]
I’m getting 372 but I don’t get how

int merge(vector<int> a, vector<int> tmp, int s, int mid, int e){
    int cnt = 0;
    int i, j, k;
    i=s;
    j=mid;
    k=s;
    while((i<= mid-1) && (j <= e)){
        if(a[i] <= a[j]){
            tmp[k++] = a[i++];
        }else{
            tmp[k++] = a[j++];
            cnt = cnt + (mid - i);
        }
    }
    while(i<= mid-1){
        tmp[k++] = a[i++];
    }
    while(j<= e){
        tmp[k++] = a[j++];
    }
    return cnt;
}

int mergeSort(vector<int> a, vector<int> tmp, int s, int e){
    int cnt = 0;
    int mid;
    if(e > s){
        mid = (s+e)/2;
        cnt += mergeSort(a,tmp,s,mid);
        cnt += mergeSort(a,tmp, mid+1,e);
        
        cnt += merge(a,tmp,s,mid+1,e);
    }
    return cnt;
}

int Solution::countInversions(vector<int> &a) {
    int n = a.size();
    vector<int> tmp(n);
    return mergeSort(a,tmp,0,n-1);
}


#2

pass the vector by reference. That worked for me.