The number of inversions for the array indicates how far (or close) the array is sorted. If the array is already sorted, then the number of inversions is 0. If the array is sorted in the reverse order, then the number of inversions is the maximum. Formally speaking, two elements a [i] and a [j] form an inversion if a [i]> a [j] and i <j Example: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1 ), (4, 1), (4, 3).
Now in O (n log n) there are various algorithms.
There is a special case when an array has only 3 types of elements - 1, 2 and 3. Now can we calculate the inversions in O (n)?
For example, 1,1,3,2,3,1,3
source
share