PHP 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 Implemenation

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;
}

Php Implementation

<?php
$a=array("a "=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

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

<?php
function getInvCount($arr, $n){
	$invcount = 0;

	for ($i = 1; $i < $n - 1; $i++){
		$small = 0;
		for($j = $i + 1; $j < $n; $j++)
			if ($arr[$i] > $arr[$j])
				$small++;
		$great = 0;
		for($j = $i - 1; $j >= 0; $j--)
			if ($arr[$i] < $arr[$j])
				$great++;
		$invcount += $great * $small;
	}

	return $invcount;
}
	$arr = array(16, 7, 22, 10);
	$n = sizeof($arr);
	echo "Inversion Count : "
		, getInvCount($arr, $n);
?>

Output

Inversion Count : 0

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

<?php
function getInvCount($arr, $n){
	$invcount = 0;

	for ($i = 1; $i < $n - 1; $i++){
		$small = 0;
		for ($j = $i + 1; $j < $n; $j++)
			if ($arr[$i] > $arr[$j])
				$small++;
		$great = 0;
		for ($j = $i - 1; $j >= 0; $j--)
			if ($arr[$i] < $arr[$j])
				$great++;
		$invcount += $great * $small;
	}

	return $invcount;
}

$arr = array (81, 14, 22, 7);
$n = sizeof($arr);
echo "Inversion Count For The Input Is : " ,
	getInvCount($arr, $n);

?>

Output

Inversion Count For The Input Is : 2

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 is denoted as O(n).

Example 3

<?php
function getInvCount($arr, $n) {
	$invcount = 0;

	for ($i = 1; $i < $n - 1; $i++){
		$small = 0;
		for ($j = $i + 1; $j < $n; $j++)
			if ($arr[$i] > $arr[$j])
				$small++;
		$great = 0;
		for ($j = $i - 1; $j >= 0; $j--)
			if ($arr[$i] < $arr[$j])
				$great++;
		$invcount += $great * $small;
	}

	return $invcount;
}
$arr = array (811, 411, 16, 7);
$n = sizeof($arr);
echo "Inversion Count After The Process : " ,
	getInvCount($arr, $n);
?>

Output

Inversion Count After The Process : 4

Conclusion

In this article, we come to know 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: 06-Apr-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements