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


In this tutorial, we will learn to count inversions of size three in a given array.

Problem statement − We have given an array of length n containing the different numeric entries. We need to find the total number of pairs of numbers of size 3 such that arr[i] > arr[j] > arr[k], where I < j < k.

Here, we will learn the brute force approach first and after that, we will optimize its time and space complexity.

Using the Brute Force Approach

In the brute force approach, we will use three nested for loops to find a count inversion of size three. The first loop makes iterations for 1 to n-2 elements and the second loop iterates from the ith element to the n-1th element. If the previous element is greater than the next element, iterate through the array and find the smaller element than the mid element.

Syntax

Users can follow the syntax below to use the brute force approach to count inversions of size three in a given array.

for ( ) {
   for ( ) {
      if (array[m] > array[n]) {
         for (let o = n + 1; o < len; o++) {
            if (array[n] > array[o])
            cnt++;
         }
      }
   }
}

Algorithm

  • Step 1 − Use the for loop to iterate through the first n-2 elements.

  • Step 2 − Use the nested for loop to iterate through the m+1 to len-1 elements.

  • Step 3 − In the nested for loop, check if array[m] is greater than array[n]. If yes, iterate through the n+1th element to the last element.

  • Step 4 − If the element at the oth index is smaller than the element at the nth index, we can say that we find a valid inversion pair of size three and increase the value of the ‘cnt’ variable by 1.

  • Step 5 − Return the value of ‘cnt’ once all iterations of for loop are completed.

Example 1

In the example below, we have implemented the brute force approach to find the total number of inversion pairs of size three.

In the given array, users can observe only 2 inversion pairs in the output. The first inversion pair is (10, 5, 4), and the second inversion pair is (20, 5, 4).

<html>
<body>
   <h3> Using the <i> Brute force approach </i> to Count Inversions of size three in a given array </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function InversionCount(array) {
         let len = array.length;
         let cnt = 0;
         for (let m = 0; m < len - 2; m++) {
            for (let n = m + 1; n < len - 1; n++) {
            if (array[m] > array[n]) {
                  for (let o = n + 1; o < len; o++) {
                     if (array[n] > array[o])
                     cnt++;
                  }
               }
            }
         }
         return cnt;
      }
      let array = [10, 20, 5, 4, 50, 60, 30, 40];
      output.innerHTML += "The count of inversion in the " + array + " is  " + InversionCount(array)
   </script>
</body>
</html>

Time and Space Complexity

  • Time complexity − The time complexity is of O(n^3) as we use three nested for loops.

  • Space complexity − The space complexity is of O(1) as we use the constant space.

Using the two Nested for Loops

In this approach, we will use two nested loops. We will find the total number of smaller elements on the right of the current element, and a total number of greater elements on the left. After that, we will multiply both to get the total number of inversions for the particular number.

Syntax

Users can follow the syntax below to use two nested loops to count the inversions of size three in JavaScript.

for ( ) {  
   // find a smaller element on the right  
   for ()
   if (array[m] < array[n])
   right++;
   
   // find bigger elements on the left
   for ()
   if (array[m] > array[n])
   left++;        
   cnt += right * left;
}

Algorithm

  • Step 1 − Use the for loop to iterate through n elements of the array.

  • Step 2 − Use the for loop to find all the smaller elements than the current element at the right side of the current element.

  • Step 3 − Use the for loop again to find all greater elements than the current element at the left side of the current element.

  • Step 4 − Multiply the value of the right and left variables, and add it to the ‘cnt’ variable.

Example 2

In the example below, we have used two nested loops to find the total number of inversions of size three, as shown in the above approach. Users can observe that the output gives the same output as in the first approach.

<html>
<body>
   <h3> Using the <i> two nested loops </i> to Count Inversions of size three in a given array </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function InversionCount(array) {
         let cnt = 0;
         let len = array.length;
         
         // Iterate through every element of the array
         for (let m = 0; m < len - 1; m++) {
         
            // count all element that are smaller than arr[m] and at the right to it
            let right = 0;
            for (let n = m - 1; n >= 0; n--)
            if (array[m] < array[n])
            right++;
            
            // count all element that are greater than arr[m] and at the left to it
            let left = 0;
            for (let n = m + 1; n < len; n++)
            if (array[m] > array[n])
            left++;
            
            // multiply left greater and right smaller elements
            cnt += right * left;
         }
         return cnt;
      }
      let array = [10, 20, 5, 4, 50, 60, 30, 40];
      output.innerHTML += "The count of inversion in the " + array + " is  " + InversionCount(array)
   </script>
</body>
</html>

Time and Space Complexity

  • Time complexity − The time complexity of the above approach is of O(n^2) as we use two nested loops.

  • Space complexity − The space complexity is of O(1) as we use constant space.

Users learned two approaches to finding the count inversions of size three in the given array. In the first approach, we solved the problem using the brute force approach, and in the second approach, we further optimized the solution to decrease the time complexity.

Updated on: 20-Apr-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements