Introduction

The Count Inversions in an Array problem is an important array and sorting problem that helps us understand how Merge Sort can be modified to solve problems efficiently.

An inversion occurs when an element appears before a smaller element.

For an array, a pair of indices i and j forms an inversion if:

i < j and arr[i] > arr[j]

The goal is to count the total number of such pairs.

Problem Statement

Given an array of integers, count the number of inversions in the array.

An inversion is a pair of elements where the first element is greater than the second element and appears before it.

Example

Input:arr = [5, 3, 2, 4, 1]

Output:
8
Inversions:
(5,3), (5,2), (5,4), (5,1),
(3,2), (3,1),
(2,1),
(4,1)

Constraints

  • 1 ≤ n ≤ 10⁵
  • Array elements can be positive, negative, or zero.
  • Duplicate elements are not counted as inversions.

Approach 1: Brute Force

Explanation

The simplest approach is to check every possible pair of elements.

For every element, compare it with all elements that come after it.

If the first element is greater than the second element, we found an inversion.

Steps

  1. Start from the first element.
  2. Compare it with every element after it.
  3. If the first element is greater than the second, increase the inversion count.
  4. Repeat for every element.
  5. Return the total count.

Dry Run

Array = [5, 3, 2, 4, 1]
5 > 3 → inversion
5 > 2 → inversion
5 > 4 → inversion
5 > 1 → inversion
3 > 2 → inversion
3 > 4 → No
3 > 1 → inversion
2 > 4 → No
2 > 1 → inversion
4 > 1 → inversion
Total = 8

Brute Force Code

Complexity Analysis

Time Complexity: O(n²)

Space Complexity: O(1)

Approach 2: Optimized Solution Using Merge Sort

Explanation

The brute-force approach checks every pair, which takes O(n²) time.

We can improve this using Merge Sort.

While merging two sorted halves, suppose:

left[i] > right[j]

Since the left half is already sorted, every remaining element from left[i] onward will also be greater than right[j].

Therefore, instead of counting them one by one, we can count all of them at once.

The number of inversions added is:

mid - i + 1

This allows us to count inversions while performing Merge Sort.

Steps

  1. Divide the array into two halves.
  2. Recursively count inversions in the left half.
  3. Recursively count inversions in the right half.
  4. Merge the two sorted halves.
  5. During merging, count cross-half inversions.
  6. Add all three counts.
  7. Return the total number of inversions.

Dry Run

Array = [5, 3, 2, 4, 1]
Divide:
[5, 3, 2] [4, 1]
Left side:
[5, 3] → [5] [3]
During merge:
5 > 3
Add 1 inversion.
Result:
[3, 5]
Merge [3, 5] with [2]:
3 > 2 → 2 inversions
5 > 2 → 1 inversion
Add 2 + 1 = 2 more inversions.
Right side:
[4, 1]
4 > 1 → 1 inversion.

Now merge the sorted halves:

[2, 3, 5] and [1, 4]
2 > 1 → 3 inversions
3 > 1 → 2 inversions
5 > 1 → 1 inversion
Cross inversions = 3
Total:

3 + 1 + 3 = 8

Optimized Code

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Edge Cases

  • An empty array has 0 inversions.
  • An array with one element has 0 inversions.
  • A sorted array has 0 inversions.
  • A reverse-sorted array has the maximum number of inversions.
  • Duplicate elements are not counted as inversions.

Why This Problem is Important

This problem demonstrates how Merge Sort can be modified to solve problems beyond simply sorting an array.

It is especially useful for understanding:

  • Divide and conquer
  • Merge Sort
  • Counting during merging
  • Efficient pair counting
  • Array inversion concepts

Real-World Applications

Inversion counting can be useful in:

  • Comparing rankings
  • Measuring how different two orderings are
  • Detecting disorder in sequences
  • Ranking and recommendation systems
  • Data analysis

Common Mistakes

  1. Counting pairs where the first element is smaller.
  2. Forgetting that the first index must be smaller than the second.
  3. Counting equal elements as inversions.
  4. Using an integer type that may overflow for large arrays.
  5. Forgetting to add mid - i + 1 during the merge step.
  6. Modifying the merge logic incorrectly and losing the sorted order.

Interview Tips

  • Clearly define what an inversion means before writing code.
  • Start with the brute-force solution.
  • Explain why O(n²) can become inefficient.
  • Explain how Merge Sort allows multiple inversions to be counted together.
  • Remember the key formula:

Number of inversions = mid - i + 1

when the current element from the right half is smaller than the current element from the left half.

Related Questions

  • Merge Sort
  • Merge Two Sorted Arrays
  • Count Smaller Numbers After Self
  • Reverse Pairs
  • Sort an Array
  • Count Smaller Elements

Final Takeaway

The Count Inversions in an Array problem can be solved by checking every pair in O(n²) time.

By modifying Merge Sort, we can count inversions efficiently in O(n log n) time.

The key idea is that when merging two sorted halves, one comparison can identify multiple inversions at once.