HackerEarth Puchi and Luggage problem solution YASH PAL, 31 July 2024 In this HackerEarth Puchi and Luggage problem solution Puchi hates to carry luggage, but unfortunately he got a job to carry the luggage of his N friends in office. Each day, one of his N friends, gives him the luggage of a particular weight to carry. You will be given the weight of luggage of each friend in the array Weight, where Weighti is the weight of luggage of ith friend carried by Puchi on ith day. It is given that all the luggages carried by Puchi are distinct in their weights. As Prateek assigned this job to Puchi, so for each day, he wants to know the number of days in future when Puchi will have to carry the luggage , having weight less than the weight of luggage of current day. Please help Prateek for the same. HackerEarth Puchi and Luggage problem solution. #include<bits/stdc++.h>#define ll long longusing namespace std;const int MAX = 100005;const int MAX1 = 1000005;int T,N;int Weight[MAX],Frequency[MAX1],Aux[MAX],Copied[MAX],F[MAX1];ll inv;void mergeFunc(int left,int mid,int right){ int p=left,q=mid+1,k=0,coun=0; for(int i=left;i<=right;i++) { if(p>mid) { Aux[k++] = Weight[q++]; } else if(q>right) { Aux[k++] = Weight[p]; Frequency[Weight[p]] += coun; p++; } else if(Weight[p] <= Weight[q]) { Aux[k++] = Weight[p]; Frequency[Weight[p]] += coun; p++; } else { Aux[k++] = Weight[q++]; inv += mid-p+1; coun++; } } for(int i=0;i<k;i++) { Weight[left++] = Aux[i]; }}void mergeSort(int left,int right){ if(left<right) { int mid = (left+right)/2; mergeSort(left,mid); mergeSort(mid+1,right); mergeFunc(left,mid,right); }}int main(){ // freopen("input10.txt","r",stdin); //freopen("output10.txt","w",stdout); for(scanf("%d",&T);T;--T) { inv =0 ; for(int i=0;i<MAX1;i++) Frequency[i] = 0; scanf("%d",&N); for(int i=0;i<N;i++) { scanf("%d",&Weight[i]); Copied[i] = Weight[i]; } mergeSort(0,N-1); ll sum = 0; for(int i=0;i<N;i++) { if(i != N-1) printf("%d ",Frequency[Copied[i]]); else printf("%dn",Frequency[Copied[i]]); } } return 0;} Second solution #include <algorithm>#include<bits/stdc++.h>#include <cstdio>#include <cstring> using namespace std; typedef long long llong; const int MAXN = 5000200;llong tree[MAXN], A[MAXN], B[MAXN]; llong read(int idx){ llong sum = 0; while (idx > 0){ sum += tree[idx]; idx -= (idx & -idx); } return sum;} void update(int idx ,llong val){ while (idx <= MAXN){ tree[idx] += val; idx += (idx & -idx); }} int main(int argc, char *argv[]) { //ios::sync_with_stdio(false); int t,i,j; cin>>t; while(t--){ int N; cin>>N; memset(tree, 0, sizeof(tree)); for(int i = 0; i < N; ++i) { cin>>A[i]; B[i] = A[i]; } sort(B, B + N); for(int i = 0; i < N; ++i) { int rank = int(lower_bound(B, B + N, A[i]) - B); A[i] = rank + 1; } llong inv_count = 0; vector<int>v(N); for(int i = N - 1; i >= 0; --i) { llong x = read(A[i] - 1); v[i]=x; inv_count += x; update(A[i], 1); } int i; for(i=0;i<N;i++) printf("%d ",v[i]); printf("n"); } return 0;} coding problems