# Python Program to Count Inversions of Size Three in A Given Array

Inversion count is a step counting method by which we can calculate the number of sorting steps taken by a particular array. It is also capable to count the operation time span for an array. But, if we want to sort an array in a reverse manner, the count will be maximum number present in that array.

Array: { 5, 4, 3, 2, 1}  // for the reverse manner
Pairs: {5, 4}, {5,3} , {3,2}, {3,1}, {2,1},{4,3}, {4,2}, {4,1},}, {5,2}, {5,1}
Output: 10
Array: {1, 2, 3, 4, 5}  // for the increasing manner
Pairs: No Pairs
Output: 0
Array: {1,5,2,8,3,4}
Pairs: {5, 2}, {5, 3}, {5, 4}, {8, 3}, {8, 4}
Output: 5


The inversion count indicates that how far that particular array is from being sorted in an increasing order. Here are two particular process to describe this situation attached with a solution −

• To find the smaller elements: To find out the smaller element from an array, we need to iterate the index from n-1 to 0. By applying (a[i]-1), we can calculate the getSum() here. The process will run until it reach to a[i]-1.

• To find the greater number: To find the greater number from an index we need to perform iteration 0 to n-1. For the every element we need to do calculation for every number till a[i]. Subtract it from i. Then we will get a the number which is greater than a[i].

## Algorithm to count inversions of size three in an array

Here in this algorithm; we learn how to count inversions of size three in a given array in a particular programming environment.

• Step 1 − Start

• Step 2 − Declare an array and inversion count (As arr[] --> array and invCount --> Inversion count)

• Step 3 − Inner loop y=x+1 to N

• Step 4 − If element at x is greater than element at y index

• Step 5 − Then, increase the invCount++

• Step 6 − Print the pair

• Step 7 − Terminate

### Syntax to count inversions of size three in an array:-

A pair (A[i], A[j]) is said to be in inversion if: A[i] > A[j] and i < j

C++ Implementation

int getInversions(int * A, int n) {
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (A[i] > a[j]) {
++count;
}
}
}
return count;
}


Java Implementation

public static int getInversions(int[] A, int n) {
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (A[i] > A[j]) {
count += 1;
}
}
}
return count;
}


Python Implementation

def getInversions(A, n):
count = 0
for i in range(n):
for j in range(i + 1, n):
if A[i] > A[j]:
count += 1
return count;


Here we have mentioned the possible syntaxes to count inversions of size three in a given array. And for this method; Time Complexity is O(N^2), where N is the total size of the array and; Space Complexity:O(1), as no extra space has been used.

### Approaches to follow

• Approach 1 − Count Inversions of size three in a given array by program to count inversions of size 3

• Approach 2 − Better Approach to count inversions of size 3

• Approach 3 − Count inversions of size 3 using binary indexed tree

## Count Inversions of size three in a given array by program to count inversions of size 3

For the simple approach to count inversions of size three, we need to run a loop for all possible value of i, j and k. The time complexity is O(n^3) and O(1) reflects the auxiliary space.

The condition is −

a[i] > a[j] > a[k] and i < j < k.

### Example 1

def getInvCount(arr):
n = len(arr)
invcount = 0
for i in range(0,n-1):
for j in range(i+1 , n):
if arr[i] > arr[j]:
for k in range(j+1 , n):
if arr[j] > arr[k]:
invcount += 1
return invcount
arr = [7 , 16, 2 , 1]
print ("Inversion Count after the operation: %d" %(getInvCount(arr)))


### Output

Inversion Count after the operation: 2


## Better Approach to count inversions of size 3

In this method we will consider the every element of an array as middle element of inversion. It helps to reduce the complexity. For this approach, the time complexity is O(n^2) and auxiliary Space is O(1).

### Example 2

def getInvCount(arr, n):
invcount = 0

for i in range(1,n-1):
small = 0
for j in range(i+1 ,n):
if (arr[i] > arr[j]):
small+=1
great = 0;
for j in range(i-1,-1,-1):
if (arr[i] < arr[j]):
great+=1
invcount += great * small

return invcount
arr = [8, 4, 2, 1]
n = len(arr)
print("Inversion Count After The Method Run :",getInvCount(arr, n))


### Output

Inversion Count After The Method Run : 4


## Count inversions of size 3 using binary indexed tree

In this method, we count the greater elements and smaller ones too. Then perform the multiply operation greater[] to smaller[] and add it to the final result. Here the time complexity is O(n*log(n)) and auxiliary space denoted as O(n).

### Example 3

def getSum( BITree, index):
sum = 0
while (index > 0):
sum += BITree[index]
index -= index & (-index)

return sum
def updateBIT(BITree, n, index, val):
while (index <= n):
BITree[index] += val
index += index & (-index)
def getInvCount(arr, n):

invcount = 0
maxElement = max(arr)
BIT =  * (maxElement + 1)
for i in range(n - 1, -1, -1):

invcount += getSum(BIT, arr[i] - 1)
updateBIT(BIT, maxElement, arr[i], 1)
return invcount
if __name__ =="__main__":
arr = [8, 4, 2, 1]
n = 4
print("Inversion Count After The Operation Done : ",
getInvCount(arr, n))


### Output

Inversion Count After The Operation Done :  6


## Conclusion

From the above discussion, we have learnt how to count inversions of size three in a given array. Hope with this article and the mentioned codes using the particular language, you have got a broad view about this topic.

Updated on: 13-Apr-2023

229 Views 