Back to problems

Count inversions in a permutation

Algorithm · Hudson River Trading · Medium

A zero-indexed array a contains n distinct integers, so it is a permutation of some set of unique values (for instance, the integers from 1 through n). For two positions i and j, the pair (i, j) is called an inversion when i a[j]. Implement a function that takes a and returns the total number of inversion pairs. Example 1: Explanation: Every earlier value is smaller than every later value, so no pair satisfies the inversion condition. Example 2: Explanation: The array is…

Checking your access…