Counting inversions in an subarrays
Last Updated :
23 Jul, 2025
Given an array arr[], the goal is to count the number of inversions in all the sub-arrays. An inversion is a pair of indices i and j such that i > j and arr[i] < arr[j]. A sub-array from index x to y ( x<= y) consists of the element's arr[x], arr[x+1], ..., arr[y]. The array arr[] can also contain repeated elements.
Examples:
Input: arr[] = {3, 6, 1, 6, 5, 3, 9}
Output:
0 0 2 2 4 7 7
0 0 1 1 3 6 6
0 0 0 0 1 3 3
0 0 0 0 1 3 3
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Explanation:
The element in the i'th row and j'th column of the output denotes the number of inversions from index i to j (inclusive). Consider i = 1 and j = 4 (assuming 0-based indexing), the number of inversions in the subarray {6, 1, 6, 5} is 3. The element pairs are (6, 1), (6, 5) and (6, 5) (from the second 6).
Input: arr[] = {3, 2, 1}
Output:
0 1 3
0 0 1
0 0 0
Explanation:
From index 0 to 1 there is 1 inversion, from index 2 to 3 there is 1 inversion and from index 0 to 2 there are 3 inversions. The i'th row and j'th column of the output is 0 if i >= j.
Naive Approach: A naive approach is to generate all possible sub-arrays and count the number of inversions in each of the sub-arrays.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// inversions in all the sub-arrays
void findSubarrayInversions(int arr[], int n)
{
int inversions[n][n];
// Initializing the inversion count
// of each subarray to 0
// inversions[i][j] will denote
// the number of inversions
// from index i to index j inclusive
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
inversions[i][j] = 0;
}
}
// Generating all subarray
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int ans = 0;
// counting the number of inversions
// for a subarray
for (int x = i; x <= j; x++) {
for (int y = x; y <= j; y++) {
if (arr[x] > arr[y])
ans++;
}
}
inversions[i][j] = ans;
}
}
// Printing the number of inversions
// of all subarrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << inversions[i][j] << " ";
}
cout << "\n";
}
}
// Driver Code
int main()
{
// Given Input
int n = 7;
int arr[n] = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
Java
// Java program for the above approach
class GFG{
// Function to count the number of
// inversions in all the sub-arrays
static void findSubarrayInversions(int arr[], int n)
{
int [][]inversions = new int[n][n];
// Initializing the inversion count
// of each subarray to 0
// inversions[i][j] will denote
// the number of inversions
// from index i to index j inclusive
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
inversions[i][j] = 0;
}
}
// Generating all subarray
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
int ans = 0;
// Counting the number of inversions
// for a subarray
for(int x = i; x <= j; x++)
{
for(int y = x; y <= j; y++)
{
if (arr[x] > arr[y])
ans++;
}
}
inversions[i][j] = ans;
}
}
// Printing the number of inversions
// of all subarrays
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(inversions[i][j] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String args[])
{
// Given Input
int n = 7;
int []arr = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
}
// This code is contributed by SoumikMondal.
Python3
# Python3 program for the above approach
# Function to count the number of
# inversions in all the sub-arrays
def findSubarrayInversions(arr, n):
inversions = [[0 for i in range(n)]
for j in range(n)]
# Initializing the inversion count
# of each subarray to 0
# inversions[i][j] will denote
# the number of inversions
# from index i to index j inclusive
# Generating all subarray
for i in range(0, n):
for j in range(0, n):
ans = 0
# Counting the number of inversions
# for a subarray
for x in range(i, j + 1):
for y in range(x, j + 1):
if (arr[x] > arr[y]):
ans += 1
# Print(ans)
inversions[i][j] = ans
# Printing the number of inversions
# of all subarrays
for i in range(0, n):
for j in range(0, n):
print(inversions[i][j], end = " ")
print()
# Driver Code
# Given Input
n = 7
arr = [ 3, 6, 1, 6, 5, 3, 9 ]
# Function Call
findSubarrayInversions(arr, n)
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count the number of
// inversions in all the sub-arrays
static void findSubarrayInversions(int []arr, int n)
{
int [,]inversions = new int[n,n];
// Initializing the inversion count
// of each subarray to 0
// inversions[i][j] will denote
// the number of inversions
// from index i to index j inclusive
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
inversions[i,j] = 0;
}
}
// Generating all subarray
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
int ans = 0;
// counting the number of inversions
// for a subarray
for (int x = i; x <= j; x++) {
for (int y = x; y <= j; y++) {
if (arr[x] > arr[y])
ans++;
}
}
inversions[i,j] = ans;
}
}
// Printing the number of inversions
// of all subarrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(inversions[i,j] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
// Given Input
int n = 7;
int []arr = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
}
// This code is contributed by ipg2016107.
JavaScript
<script>
// JavaScript program for the above approach
// Function to count the number of
// inversions in all the sub-arrays
function findSubarrayInversions(arr, n) {
let inversions = new Array(n).fill(0).map(() => new Array(n));
// Initializing the inversion count
// of each subarray to 0
// inversions[i][j] will denote
// the number of inversions
// from index i to index j inclusive
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
inversions[i][j] = 0;
}
}
// Generating all subarray
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
let ans = 0;
// counting the number of inversions
// for a subarray
for (let x = i; x <= j; x++) {
for (let y = x; y <= j; y++) {
if (arr[x] > arr[y])
ans++;
}
}
inversions[i][j] = ans;
}
}
// Printing the number of inversions
// of all subarrays
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(inversions[i][j] + " ");
}
document.write("<br>");
}
}
// Driver Code
// Given Input
let n = 7;
let arr = [3, 6, 1, 6, 5, 3, 9];
// Function Call
findSubarrayInversions(arr, n);
</script>
Output: 0 0 2 2 4 7 7
0 0 1 1 3 6 6
0 0 0 0 1 3 3
0 0 0 0 1 3 3
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Time Complexity: O(N4)
Auxiliary Space: O(N2)
Efficient Approach: The above method can be optimized a little using the method given here to find the number of inversions in a sub-array. First see some observations to solve this problem:
First create a 2d array greater[][], where greater[i][j] denotes the number of elements in the range i to j which are greater than arr[i]. Iterate over the array and for each element, find the number of elements to its right which are less than the element. This can be done using a naive approach in O(N^2). Now to find the number of inversions in a range say x to y, the answer will be greater[x][y] + greater[x+1][y] + ... + greater[y-1][y] + greater[y][y].
With the greater[][] table this value can be calculated in O(n) for each sub-array resulting in a complexity of O(n^3).(There are O(n^2) sub-array, and it takes O(n) time to compute the number of inversions in each sub-array). To find this value in O(1) each time build a prefix sum table from the greater[][] table where prefix[i][j] denotes the value of greater[0][j] + greater[1][j] + ... + greater[i][j]. This table can also be built in O(N^2) time. Now the answer for each sub-array from x to y (inclusive) would become prefix[y][y] - prefix[x-1][y] if x >= 1 and prefix[y][y] if x = 0.
Follow the step below to solve this problem:
- Initialize arrays greater[][] to store where greater[i][j] denotes the number of elements in the range i to j which are greater than arr[i], prefix[][] to store prefix sum for sub-array and inversions[][] to store the number of inversions.
- Iterate in a range[0, N-1] using a variable i:
- Iterate in a range[i+1, N-1] using a variable j:
- Update greater[i][j] as greater[i][j-1]
- If arr[i] is greater than arr[j], then Increment greater[i][j] by 1.
- Iterate in a range[0, N-1] using a variable i:
- Update prefix[0][j] as greater[0][j]
- Iterate in a range[1, N-1] using a variable j and update prefix[i][j] as prefix[i-1][j] + greater[i][j].
- Iterate in a range[0, N-1] using a variable i:
- Iterate in a range[i, N-1] using a variable j:
- If i = 0, then update inversions[i][j] as prefix[j][j]
- Otherwise, update inversions[i][j] as prefix[j][j] + prefix[i-1][j].
- After completing the above steps, print inversions[][] array as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// inversions in all the sub-arrays
void findSubarrayInversions(int arr[], int n)
{
int greater[n][n];
int prefix[n][n];
int inversions[n][n];
// Initializing the arrays to 0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
greater[i][j] = 0;
prefix[i][j] = 0;
inversions[i][j] = 0;
}
}
// For each pair of indices i and j
// calculating the number of elements
// from i to j inclusive which are
// greater than arr[i]
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
greater[i][j] = greater[i][j - 1];
if (arr[i] > arr[j])
greater[i][j]++;
}
}
// Building the prefix table.
// Prefix[i][j] denotes the sum of
// greater[0][j], greater[1][j] ... greater[i][j]
for (int j = 0; j < n; j++) {
prefix[0][j] = greater[0][j];
for (int i = 1; i < n; i++) {
prefix[i][j] = prefix[i - 1][j] + greater[i][j];
}
}
// Calculating the inversion count for
// each subarray using the prefix table
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 0)
inversions[i][j] = prefix[j][j];
else
inversions[i][j] = prefix[j][j]
- prefix[i - 1][j];
}
}
// Printing the values of the number
// of inversions in each subarray
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << inversions[i][j] << " ";
}
cout << "\n";
}
}
// Driver Code
int main()
{
// Given Input
int n = 7;
int arr[n] = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to count the number of
// inversions in all the sub-arrays
static void findSubarrayInversions(int arr[], int n)
{
int greater[][] = new int[n][n];
int prefix[][] = new int[n][n];
int inversions[][] = new int[n][n];
// Initializing the arrays to 0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
greater[i][j] = 0;
prefix[i][j] = 0;
inversions[i][j] = 0;
}
}
// For each pair of indices i and j
// calculating the number of elements
// from i to j inclusive which are
// greater than arr[i]
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
greater[i][j] = greater[i][j - 1];
if (arr[i] > arr[j])
greater[i][j]++;
}
}
// Building the prefix table.
// Prefix[i][j] denotes the sum of
// greater[0][j], greater[1][j] ... greater[i][j]
for (int j = 0; j < n; j++) {
prefix[0][j] = greater[0][j];
for (int i = 1; i < n; i++) {
prefix[i][j] = prefix[i - 1][j] + greater[i][j];
}
}
// Calculating the inversion count for
// each subarray using the prefix table
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 0)
inversions[i][j] = prefix[j][j];
else
inversions[i][j] = prefix[j][j]
- prefix[i - 1][j];
}
}
// Printing the values of the number
// of inversions in each subarray
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(inversions[i][j]+" ");
}
System.out.println();
}
}
public static void main (String[] args) {
int n = 7;
int arr[] = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
}
// This code is contributed by aadityaburujwale.
Python3
# Python3 program for the above approach
# Function to count the number of
# inversions in all the sub-arrays
def findSubarrayInversions(arr, n):
# Initializing the arrays to 0
greater = [[0 for i in range(n)]
for j in range(n)]
prefix = [[0 for i in range(n)]
for j in range(n)]
inversions = [[0 for i in range(n)]
for j in range(n)]
# For each pair of indices i and j
# calculating the number of elements
# from i to j inclusive which are
# greater than arr[i]
for i in range(0, n):
for j in range(i + 1, n):
greater[i][j] = greater[i][j - 1]
if (arr[i] > arr[j]):
greater[i][j] += 1
# Building the prefix table.
# Prefix[i][j] denotes the sum of
# greater[0][j], greater[1][j] ... greater[i][j]
for j in range(0, n):
prefix[0][j] = greater[0][j]
for i in range(1, n):
prefix[i][j] = (prefix[i - 1][j] +
greater[i][j])
# Calculating the inversion count for
# each subarray using the prefix table
for i in range(0, n):
for j in range(i, n):
if (i == 0):
inversions[i][j] = prefix[j][j]
else:
inversions[i][j] = (prefix[j][j] -
prefix[i - 1][j])
# Printing the values of the number
# of inversions in each subarray
for i in range(0, n):
for j in range(0, n):
print(inversions[i][j], end = " ")
print()
# Driver Code
# Given Input
n = 7
arr = [ 3, 6, 1, 6, 5, 3, 9 ]
# Function Call
findSubarrayInversions(arr, n)
# This code is contributed by amreshkumar3
C#
using System;
public class GFG {
// Function to count the number of
// inversions in all the sub-arrays
static void findSubarrayInversions(int[] arr, int n)
{
int[, ] greater = new int[n, n];
int[, ] prefix = new int[n, n];
int[, ] inversions = new int[n, n];
// Initializing the arrays to 0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
greater[i, j] = 0;
prefix[i, j] = 0;
inversions[i, j] = 0;
}
}
// For each pair of indices i and j
// calculating the number of elements
// from i to j inclusive which are
// greater than arr[i]
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
greater[i, j] = greater[i, j - 1];
if (arr[i] > arr[j])
greater[i, j]++;
}
}
// Building the prefix table.
// Prefix[i][j] denotes the sum of
// greater[0][j], greater[1][j] ... greater[i][j]
for (int j = 0; j < n; j++) {
prefix[0, j] = greater[0, j];
for (int i = 1; i < n; i++) {
prefix[i, j]
= prefix[i - 1, j] + greater[i, j];
}
}
// Calculating the inversion count for
// each subarray using the prefix table
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 0)
inversions[i, j] = prefix[j, j];
else
inversions[i, j]
= prefix[j, j] - prefix[i - 1, j];
}
}
// Printing the values of the number
// of inversions in each subarray
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(inversions[i, j] + " ");
}
Console.WriteLine();
}
}
public static void Main(string[] args)
{
int n = 7;
int[] arr = { 3, 6, 1, 6, 5, 3, 9 };
// Function Call
findSubarrayInversions(arr, n);
}
}
// This code is contributed by krandeep1234.
JavaScript
<script>
// Javascript program for the above approach
// Function to count the number of
// inversions in all the sub-arrays
function findSubarrayInversions(arr, n) {
let greater = new Array(n).fill(0).map(() => new Array(n).fill(0));
let prefix = new Array(n).fill(0).map(() => new Array(n).fill(0));
let inversions = new Array(n).fill(0).map(() => new Array(n).fill(0));
// Initializing the arrays to 0
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
greater[i][j] = 0;
prefix[i][j] = 0;
inversions[i][j] = 0;
}
}
// For each pair of indices i and j
// calculating the number of elements
// from i to j inclusive which are
// greater than arr[i]
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
greater[i][j] = greater[i][j - 1];
if (arr[i] > arr[j]) greater[i][j]++;
}
}
// Building the prefix table.
// Prefix[i][j] denotes the sum of
// greater[0][j], greater[1][j] ... greater[i][j]
for (let j = 0; j < n; j++) {
prefix[0][j] = greater[0][j];
for (let i = 1; i < n; i++) {
prefix[i][j] = prefix[i - 1][j] + greater[i][j];
}
}
// Calculating the inversion count for
// each subarray using the prefix table
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
if (i == 0) inversions[i][j] = prefix[j][j];
else inversions[i][j] = prefix[j][j] - prefix[i - 1][j];
}
}
// Printing the values of the number
// of inversions in each subarray
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(inversions[i][j] + " ");
}
document.write("<br>");
}
}
// Driver Code
// Given Input
let n = 7;
let arr = [3, 6, 1, 6, 5, 3, 9];
// Function Call
findSubarrayInversions(arr, n);
// This code is contributed by saurabh_jaiswal.
</script>
Output: 0 0 2 2 4 7 7
0 0 1 1 3 6 6
0 0 0 0 1 3 3
0 0 0 0 1 3 3
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Time Complexity: O(N2)
Space Complexity: O(N2)
Notes-
- Some space can be saved by building the prefix[][] table on top of the greater[][] table but the order will still remain the same.
- It is not possible to perform better than O(N^2) if you want to find the exact count of inversions in all subarrays as the number of subarrays is always O(N^2).
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem