Home > Backend Development > PHP Tutorial > Given an array, write a PHP program to count the number of reverse pairs of size three

Given an array, write a PHP program to count the number of reverse pairs of size three

PHPz
Release: 2023-09-02 19:50:01
forward
628 people have browsed it

Given an array, write a PHP program to count the number of reverse pairs of size three

Backward counting is a step counting method by which we can count the number of sorting steps performed for a particular array. It is also able to calculate the operation time span of an array. But if we want to sort the array in the opposite way, the count will be the maximum number present in the 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  
Copy after login

The inversion count indicates how far away that particular array is from being sorted in ascending order. Here are two specific procedures describing this situation with solutions -

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

  • To find the larger number - To find the larger number from the index, we need to perform iterations 0 to n-1. For each element, we need to calculate each number until a[i]. Subtract it from i. Then we will get a number larger than a[i].

Algorithm for calculating the inversion of size 3 in an array: -

In this algorithm; we learn how to calculate the inversion of size 3 in a given array in a specific programming environment.

  • Step 1 - Get Started

  • Step 2 - Declare the array and invert the count (such as arr[] --> array and invCount --> invert the count)

  • Step 3 - Inner loop y=x 1 to N

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

  • Step 5 - Then increase invCount

  • Step 6 - Print the pair

  • Step 7 - Termination

Syntax for calculating the inversion of size 3 in an array: -

A pair (A[i], A[j]) is said to be in an inverted state if the following conditions are met: A[i] > A[j] and i < j< 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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

PHP implementation

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

Here we mentioned the possible syntax for computing the inversion of size 3 in a given array. For this method; time complexity: O(N^2), where N is the total size of the array; space complexity: O(1), since no extra space is used.

Method to follow:-

  • Method 1 - Programmatically compute the reversal of size 3 in the given array to compute the reversal of size 3

  • Method 2 - Better way to calculate the inversion of size 3

  • Method 3 - Compute the reversal of size 3 using a binary index tree

Count reversals of size 3 in a given array by program to calculate reversals of size 3

For a simple way to calculate the inversion of size 3, we need to run a loop for all possible values ​​of i, j and k. The time complexity is O(n^3), and O(1) reflects the auxiliary space.

requirement is:

a[i] > a[j] > a[k] and i < j < k. < 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);
?>
Copy after login

Output

Inversion Count : 0
Copy after login

Better way to calculate inversion of size 3

In this method, we treat each element of the array as an inverted middle element. It helps reduce complexity. For this approach, the time complexity is O(n^2) and the 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);

?>
Copy after login

Output

Inversion Count For The Input Is : 2
Copy after login

Compute the inversion of size 3 using a binary index tree

In this method, we also count the larger elements and smaller elements. Then perform the multiplication of greater[] and smaller[] and add it to the final result. The time complexity here is O(n*log(n)), and the auxiliary space is expressed 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);
?>
Copy after login

Output

Inversion Count After The Process : 4
Copy after login

in conclusion

In this article, we will see how to calculate the inversion of size 3 in a given array. Hopefully, through this article and the mentioned code using specific languages, you have gained a broad understanding of the subject.

The above is the detailed content of Given an array, write a PHP program to count the number of reverse pairs of size three. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template