## Tuesday, July 12, 2016

### Count Inversions in an array

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
Example: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
http://www.geeksforgeeks.org/counting-inversions/
http://www.geeksforgeeks.org/count-inversions-in-an-array-set-2-using-self-balancing-bst/

http://quiz.geeksforgeeks.org/counting-inversions-using-set-in-c-stl/

Variation: Find Surpasser Count of each element in array
A surpasser of an element of an array is a greater element to its right, therefore x[j] is a surpasser of x[i] if i < j and x[i] < x[j]. The surpasser count of an element is the number of surpassers. Given an array of distinct integers, for each element of the array find its surpasser count i.e. count the number of elements to the right that are greater than that element.
Examples:
Input:  [2, 7, 5, 3, 0, 8, 1]

Output: [4, 1, 1, 1, 2, 0, 0]
http://www.geeksforgeeks.org/find-surpasser-count-of-each-element-in-array/