Problem:
Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. [Courtesy: geeksforgeeks.org]
Solution:
The problem can be solved using O(n^2) algorithm like bubble sort.
- This way just perform the bubble sort on the array.
- Each time inside the loop we find a comparison between a pair that require a swap to sort, increase Inverse_Count value.
- Finally print the value of Inverse_Count.
Another approach is to use Merge Sort , an O(nlogn) algorithm.
Merge sort use two sorted array and merge these two into a bigger array. So to solve the problem using merge sort we need to do as follows:
- Let the two arrays to be merged are Left and Right array.
- If we find an element in Right array that should go before the current comparing element of Left array, than it is obvious that rest of Left array elements will go after that element of the Right array. So from here we can say Inverse_Count will be increased to Inverse_Count+(Length Of the Left Array- Current Element Index).
The problem appeared as interview Question in: Microsoft, Amazon, Adobe, FLipkart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#include <stdio.h> #include <iostream> /* 3 5 2 4 1 3 5 6 12 11 13 5 6 7 10 45 -2 -45 78 30 -42 10 19 73 93 */ using namespace std; int arr[100000]; void Merge(int lw,int mid,int hi,int *Inv_count) { int len=mid-lw+1; int len2=hi-(mid+1)+1; int L[len]; int R[len2]; for(int i=0;i<len;i++) L[i]=arr[lw+i]; for(int i=0;i<len2;i++) R[i]=arr[mid+1+i]; int i = 0, j = 0, k = lw; while(i<len && j<len2) { if(L[i]<=R[j]) { arr[k]=L[i]; k++; i++; } else { arr[k]=R[j]; (*Inv_count)+=(len-i); k++; j++; } } while(i<len) { arr[k]=L[i]; k++; i++; } while(j<len2) { arr[k]=R[j]; k++; j++; } } void MergeSort(int lw,int hi,int *Inv_count) { int mid; int c=0; if(lw<hi) { mid=lw+(hi-lw)/2; MergeSort(lw,mid,Inv_count); MergeSort(mid+1,hi,Inv_count); Merge(lw,mid,hi,Inv_count); } } int InverseCout(int st,int end) { int Inv_count=0; MergeSort(st,end,&Inv_count); return Inv_count; } int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("%d\n",InverseCout(0,n-1)); //cout<<InverseCout(0,n-1)<<endl; } return 0; } |