Count Inversions

medium

You are given an array of integers arr. Your task is to count the number of inversions in it.

An inversion is a pair of indices (i, j) such that i < j but arr[i] > arr[j] — a pair of elements that stand in the wrong order compared to the sorted array.

Return the total number of inversions. A fully sorted (non-decreasing) array has 0 inversions; a reverse-sorted array has the maximum possible, n * (n - 1) / 2.

Think of the answer as the array's messiness score — the exact number of adjacent swaps a bubble sort would need to fix it.

Hints

An inversion is simply a pair standing in the wrong order compared to the sorted array. How many pairs would you have to check naively — and is that fast enough for n up to 10^5?
The inversion count measures how far the array is from sorted. Could a sorting algorithm count the out-of-order pairs as a side effect of sorting?
Merge sort. While merging two sorted halves, the moment an element from the right half is placed before remaining left-half elements, it forms an inversion with all of them at once — add len(left) - i in one step.

Common doubts

No. An inversion requires strict arr[i] > arr[j], which is why [10, 10, 10] has zero inversions. In the merge step this means ties must go to the left half (left[i] <= right[j]) — using strict < there silently counts equal pairs.
Because each half's internal inversions are counted by the recursive call before the half is rearranged. After that, only cross-boundary pairs remain uncounted — and for those, only which values sit in each half matters, not their order, so sorting is harmless.
Up to n * (n - 1) / 2 for a reverse-sorted array — about 5 * 10^9 when n = 10^5. Accumulate the count in a 64-bit-wide type when constraints push into that territory.
The counting merge sorts a working copy (or the array in place, depending on implementation). Since the problem only asks for the count, sorting the array as a side effect is perfectly fine.

Interview follow-ups

Yes — sweep the array right to left; for each element, query the Fenwick tree for how many smaller values have already been seen, then insert the current value. With values up to 10^4 no coordinate compression is even needed. Also O(n log n), and it generalizes to online updates.
Same merge sort skeleton, but the condition no longer aligns with the merge comparison — so run a separate two-pointer counting pass over the two sorted halves before merging. That is exactly the Reverse Pairs problem.
Track indices through the merge and attribute the len(left) - i jumps to the individual right elements — this is Count of Smaller Numbers After Self, the per-element version of this exact technique.
It equals the inversion count — each adjacent swap fixes exactly one inversion, no more. So you have already solved that problem too.

Fun facts

  • The inversion count is exactly the number of swaps bubble sort performs — it is the array's edit distance from sortedness, which is why it is used to measure how nearly sorted data is.
  • In statistics, the Kendall tau distance between two rankings is an inversion count in disguise — it powers rank-correlation tests and is used to compare search-result orderings.
  • The count-during-merge trick reappears in Reverse Pairs and Count of Smaller Numbers After Self — three famous problems, one merge sort skeleton.

Asked at

AmazonMicrosoftAdobeFlipkartGoogle
Frequently Sometimes Occasionally
Example 1
Input: arr = [2, 4, 1, 3, 5]
Output: 3
Three pairs are out of order: (2, 1), (4, 1) and (4, 3).
Example 2
Input: arr = [2, 3, 4, 5, 6]
Output: 0
The array is already sorted, so no pair is out of order.
Example 3
Input: arr = [10, 10, 10]
Output: 0
Equal elements are never an inversion — the comparison is strictly greater-than.
Constraints

- 1 <= arr.size <= 10^5 - 1 <= arr[i] <= 10^4

Solve this problem →