Counting Inversions using BIT
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Counting inversions is a classic problem in computer science and combinatorial mathematics, often used to measure the difference between two sequences or to determine the disorder of an array. An inversion is defined as a situation when a pair of elements in an array is out of order. Formally, given an array `A` with indices `i` and `j`, an inversion is a pair `(i, j)` such that `i < j` and `A[i] > A[j]`.
To efficiently count inversions, especially in scenarios involving dynamic data or multiple queries, a Binary Indexed Tree (BIT), also known as a Fenwick Tree, is particularly useful due to its excellent balance between simplicity and efficiency. In this article, we'll delve into the usage of BIT for counting inversions, along with examples and technical explanations.
Understanding Binary Indexed Tree (BIT)
A Binary Indexed Tree is a data structure that provides efficient methods for cumulative frequency tables and can be built in time. It supports two main operations:
- Update: Increases the frequency of a particular element.
- Prefix Sum: Calculates the cumulative frequency up to a specific index.
The BIT is usually implemented with an array where each index contains the sum of elements in a segment of the input array. This allows both operations to be performed in time.
Counting Inversions with BIT
Algorithm Overview
To count inversions using BIT, we leverage its ability to quickly retrieve cumulative sums and update frequencies. The process involves iterating through the array and determining how many elements in the array to the right have already appeared and are smaller.
Algorithm Steps
- Normalize the Array: Convert the array into ranks. This step ensures that all elements fit within the range of the BIT. This is done by sorting the unique elements of the array and replacing each element by its rank (position in the sorted unique list).
- Initialize the BIT: Create a BIT array large enough to store frequencies for each rank.
- Iterate and Update: Traverse the original array from right to left. For each element:
- Determine its rank.
- Use the BIT to get the prefix sum of ranks smaller than the current rank, representing how many elements to the right are smaller (i.e., valid inversions).
- Update the BIT by increasing the frequency of the current rank.
Example
Consider the array `A = [3, 1, 2]`. Let's count inversions using BIT.
- Normalize: Map the array to ranks. Array `A` becomes `[2, 1, 3]`.
- Initialize BIT: Create a BIT of size 3 (since there are 3 unique ranks).
- Process and Count:
- Start from the end:
- Element `2` (rank 3): No smaller rank seen so far, add `0` to inversions and update BIT.
- Element `1` (rank 1): BIT prefix sum of ranks < 1 is `0`, update BIT for rank 1.
- Element `3` (rank 2): BIT prefix sum of ranks < 2 is `1` (element 1), implying one inversion. Update BIT with rank 2.
The total number of inversions is `1`.
Implementation
Here is a Python implementation of the above logic:
- Efficiency: With updates and prefix sum queries both in time, BIT is efficient for real-time applications.
- Flexibility: Easily handles changes in the dataset, making it suitable for dynamic lists or scenarios with multiple queries.
Related reading
- Counting minimum number of swaps to group characters in string
- Counting number of bits How does this line work ? nnn-1;
- Counting palindromic substrings in On
- Counting the adjacent swaps required to convert one permutation into another
- Counting subsets with given sizes of a set
- Counting the number of distinct keys in a dictionary in Python
- Counting the bits set in the Fibonacci number system?
- Course assignment algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.