Javascript Program to Count Inversions of size three in a given array
Last Updated :
16 Sep, 2024
Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.
Example :
Input: {8, 4, 2, 1}
Output: 4
The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1).
Input: {9, 6, 4, 5, 8}
Output: 2
The two inversions are {9, 6, 4} and {9, 6, 5}
We have already discussed the inversion count of size two by merge sort, Self Balancing BST and BIT.
Simple approach:
Loop for all possible value of i, j and k and check for the condition a[i] > a[j] > a[k] and i < j < k.
JavaScript
// A simple Javascript implementation to count inversion of size 3
// returns count of inversion of size 3
function getInvCount(arr, n) {
let invcount = 0; // initialize result
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++) {
if (arr[i] > arr[j]) {
for (let k = j + 1; k < n; k++) {
if (arr[j] > arr[k])
invcount++;
}
}
}
}
return invcount;
}
// driver program to test above function
let arr = [8, 4, 2, 1];
let n = arr.length;
console.log("Inversion count : " + getInvCount(arr, n));
// This code is contributed by rag2127
OutputInversion count : 4
Time complexity of this approach is : O(n^3)
Efficieant Approach :
We can reduce the complexity if we consider every element arr[i] as middle element of inversion, find all the numbers greater than a[i] whose index is less than i, find all the numbers which are smaller than a[i] and index is more than i. We multiply the number of elements greater than a[i] to the number of elements smaller than a[i] and add it to the result.
Below is the implementation of the idea.
JavaScript
// A O(n^2) Javascript program to count inversions of size 3
// returns count of inversion of size 3
function getInvCount(arr, n) {
let invcount = 0; // initialize result
for (let i = 0; i < n - 1; i++) {
// count all smaller elements on right of arr[i]
let small = 0;
for (let j = i + 1; j < n; j++)
if (arr[i] > arr[j])
small++;
// count all greater elements on left of arr[i]
let great = 0;
for (let j = i - 1; j >= 0; j--)
if (arr[i] < arr[j])
great++;
// update inversion count by adding all inversions
// that have arr[i] as middle of three elements
invcount += great * small;
}
return invcount;
}
// driver program to test above function
let arr = [8, 4, 2, 1];
let n = arr.length;
console.log("Inversion count : " + getInvCount(arr, n));
// This code is contributed by avanitrachhadiya2155
OutputInversion count : 4
Time Complexity of this approach : O(n^2)
Binary Indexed Tree Approach :
Like inversions of size 2, we can use Binary indexed tree to find inversions of size 3. It is strongly recommended to refer below article first.
count inversions of size two Using BIT
The idea is similar to above method. We count the number of greater elements and smaller elements for all the elements and then multiply greater[] to smaller[] and add it to the result.
Solution :
- To find out the number of smaller elements for an index we iterate from n-1 to 0. For every element a[i] we calculate the getSum() function for (a[i]-1) which gives the number of elements till a[i]-1.
- To find out the number of greater elements for an index we iterate from 0 to n-1. For every element a[i] we calculate the sum of numbers till a[i] (sum smaller or equal to a[i]) by getSum() and subtract it from i (as i is the total number of element till that point) so that we can get number of elements greater than a[i].
Please refer complete article on Count Inversions of size three in a given array for more details!
Similar Reads
Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3. Example : Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input
15+ min read
Javascript Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) 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, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers
5 min read
Count inversions of size k in a given array Given an array of n distinct integers a_{1}, a_{2}, ..., a_{n} and an integer k. Find out the number of sub-sequences of a such that a_{i_{1}} > a_{i_{2}} > ... > a_{i_{k}} , and 1 <= i_{1} < i_{2} < ... < i_{k} <= n . In other words output the total number of inversions of l
14 min read
Javascript Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl
3 min read
Counting inversions in an array using segment tree Given an array of integers arr, the task is to count the number of inversions in the array. If A[i] > A[j] and i < j then the pair (A[i], A[j]) is part of an inversion. Examples: Input: arr[] = {8, 4, 2, 1} Output: 6 Input: arr[] = {3, 1, 2} Output: 2 Approach: Build a segment tree where each
10 min read
Counting segment inversions of an Array with updates Given an array of small integers arr[] and a 2D array of queries[][], In each query array we have the first element indicating the operation type, and the remaining elements are the arguments. If query[i][0] is '1' then query[i][1] = l and query[i][2] = r and we have to Find the number of inversions
15+ min read
Check if the count of inversions of two given types on an Array are equal or not Given an array a[] on which, the following two types of inversions are performed: Count of pairs of indices (i, j) such that A[i] > A[j] and i < jCount of pairs of indices (i, j) such that A[i] > A[j] and j = i + 1 The task is to check if the count of both the inversions is equal or not. If
5 min read
Counting inversions in all subarrays of given size Given an array and an integer k, count all inversions in all subarrays of size k. Example: Input : a[] = {7, 3, 2, 4, 1}, k = 3; Output : 6 Explanation: subarrays of size 3 are - {7, 3, 2} {3, 2, 4} {2, 4, 1} and there inversion count are 3, 1, 2 respectively. So, total number of inversions are 6. I
15+ min read
Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation
4 min read
Count inversions in an array | Set 4 ( Using Trie ) Inversion Count for an array indicates â how far (or close) the array is from being sorted. If the array is already sorted then inversion count is 0. If the array is sorted in 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 <
12 min read