Open In App

Php Program to Count Inversions of size three in a given array

Last Updated : 17 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 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.
 

PHP




<?php
// A O(n^2) PHP program to 
// count inversions of size 3
  
// Returns count of 
// inversions of size 3
function getInvCount($arr, $n)
{
      
    // Initialize result
    $invcount = 0; 
  
    for ($i = 1; $i < $n - 1; $i++)
    {
          
        // Count all smaller elements 
        // on right of arr[i]
        $small = 0;
        for($j = $i + 1; $j < $n; $j++)
            if ($arr[$i] > $arr[$j])
                $small++;
  
        // Count all greater elements 
        // on left of arr[i]
        $great = 0;
        for($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 Code
    $arr = array(8, 4, 2, 1);
    $n = sizeof($arr);
    echo "Inversion Count : "
        , getInvCount($arr, $n);
  
// This code is contributed m_kit
?>


Output:

Inversion Count : 4 

Time complexity of this approach is : O(n^3)

Auxiliary Space: O(1)

As constant extra space is used.
Better 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.
 

PHP




<?php
// A O(n^2) PHP program to count
// inversions of size 3
  
// Returns count of 
// inversions of size 3
function getInvCount($arr, $n)
{
    // Initialize result
    $invcount = 0; 
  
    for ($i = 1; $i < $n - 1; $i++)
    {
        // Count all smaller elements
        // on right of arr[i]
        $small = 0;
        for ($j = $i + 1; $j < $n; $j++)
            if ($arr[$i] > $arr[$j])
                $small++;
  
        // Count all greater elements
        // on left of arr[i]
        $great = 0;
        for ($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 Code
$arr = array (8, 4, 2, 1);
$n = sizeof($arr);
echo "Inversion Count : "
      getInvCount($arr, $n);
      
// This code is contributed by m_kit
?>


Output :

Inversion Count : 4 

Time Complexity of this approach : O(n^2)

Auxiliary Space: O(1)

As constant extra space is used.
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 :

  1. 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.
  2. 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!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads