Open In App

Inversion count in Array using BIT

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
45 Likes
Like
Report

Inversion Count for an array indicates – how far (or close) the array is from being sorted. If the array is already sorted then the inversion count is 0. If the array is sorted in the reverse order that inversion count is the maximum. 
Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. For simplicity, we may assume that all elements are unique.
Example: 

Input: arr[] = {8, 4, 2, 1}
Output: 6
Explanation: Given array has six inversions: (8,4), (4,2), (8,2), (8,1), (4,1), (2,1).     

Input: arr[] = {3, 1, 2}
Output: 2
Explanation: Given array has two inversions: (3,1), (3,2).

We strongly recommend that you click here and practice it, before moving on to the solution.

We have already discussed the below methods to solve inversion count:  

  1. Naive and Modified Merge Sort
  2. Using AVL Tree

We recommend you to refer Binary Indexed Tree (BIT) before further reading this post. 
Solution using BIT of size Θ(maxElement):  

  • Approach: Traverse through the array and for every index find the number of smaller elements on the right side of the array. This can be done using BIT. Sum up the counts for all indexes in the array and print the sum.
  • Background on BIT: 
    1. BIT basically supports two operations for an array arr[] of size n: 
      1. Sum of elements till arr[i] in O(log n) time.
      2. Update an array element in O(log n) time.
    2. BIT is implemented using an array and works in form of trees. Note that there are two ways of looking at BIT as a tree. 
      1. The sum operation where parent of index x is "x - (x & -x)".
      2. The update operation where parent of index x is "x + (x & -x)".
  • Algorithm: 
    1. Create a BIT, to find the count of the smaller elements in the BIT for a given number and also a variable result = 0.
    2. Traverse the array from end to start.
    3. For every index check how many numbers less than the current element are present in BIT and add it to the result
    4. To get the count of smaller elements, getSum() of BIT is used.
    5. In his basic idea, BIT is represented as an array of size equal to maximum element plus one. So that elements can be used as an index.
    6. After that we add the current element to the BIT[] by doing an update operation that updates the count of the current element from 0 to 1, and therefore updates ancestors of the current element in BIT (See update() in BIT for details).

Below is the implementation of the above approach:

C++
Java Python3 C# JavaScript PHP

Output
Number of inversions are : 6

Time Complexity :- The update function and getSum function runs for O(log(maximumelement)). The getSum function has to be run for every element in the array. So overall time complexity is : O(nlog(maximumelement)).
Auxiliary space : O(maxElement), space required for the BIT is an array of the size of the largest element.

Better solution using BIT of size Θ(n): 
Approach: Traverse through the array and for every index find the number of smaller elements on its right side of the array. This can be done using BIT. Sum up the counts for all indexes in the array and print the sum. The approach remains the same but the problem with the previous approach is that it doesn't work for negative numbers as the index cannot be negative. The idea is to convert the given array to an array with values from 1 to n and the relative order of smaller and greater elements remains the same.
Example:

 
arr[] = {7, -90, 100, 1}
It gets converted to,
arr[] = {3, 1, 4 ,2 }
as -90 < 1 < 7 < 100.

Explanation: Make a BIT array of a number of
elements instead of a maximum element. Changing
element will not have any change in the answer
as the greater elements remain greater and at the
same position.

Algorithm: 

  1. Create a BIT, to find the count of the smaller elements in the BIT for a given number and also a variable result = 0.
  2. The previous solution does not work for arrays containing negative elements. So, convert the array into an array containing relative numbering of elements,i.e make a copy of the original array and then sort the copy of the array and replace the elements in the original array with the indices of the same elements in the sorted array. 
    For example, if the array is {-3, 2, 0} then the array gets converted to {1, 3, 2}
  3. Traverse the array from end to start.
  4. For every index check how many numbers less than the current element are present in BIT and add it to the result

Below is the implementation of the above approach: 

C++
Java Python3 C# JavaScript

Output
Number of inversions are : 6

Time Complexity: The update function and getSum function runs for O(log(n)). The getSum function has to be run for every element in the array. So overall time complexity is O(nlog(n)).
Auxiliary Space: O(n). 
Space required for the BIT is an array of the size n.


Similar Reads