A music app wants to show how "shuffled" a playlist is compared with its sorted order. One natural measure is the number of inversions: pairs of positions i < j where nums[i] > nums[j]. A sorted list has none; a list in reverse order has the most.
Write two functions.
countInversions(nums) takes an array of numbers and returns how many pairs i < j have nums[i] > nums[j].
rankingDisagreements(first, second) takes two rankings of the same people, each an array of names from best to worst, and returns how many pairs of people the two rankings put in opposite order.
Rules:
Equal values are not an inversion: [2, 2] has 0.
nums must be an array of finite numbers. Each ranking must be an array of strings. Otherwise throw a TypeError.
If the two rankings do not contain exactly the same names, or a name appears twice, throw a RangeError.
Do not modify the inputs. Empty and one-item arrays have 0 inversions.
Arrays can have 200,000 items, so there can be about 20 billion pairs. Checking every pair will not finish in time; aim for O(n log n).