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. An inversion of size three is a triplet of indices (i, j, k) where i < j < k but arr[i] > arr[j] > arr[k].

Problem Statement: Given an array of length n containing different numeric entries, we need to find the total number of triplets (i, j, k) such that arr[i] > arr[j] > arr[k], where i < j < k.

We'll explore two approaches: a brute force method and an optimized solution using nested loops.

Using the Brute Force Approach

In the brute force approach, we use three nested loops to check every possible triplet. For each element at position i, we check all elements at positions j (where j > i), and for each valid pair (i, j) where arr[i] > arr[j], we count elements at positions k (where k > j) such that arr[j] > arr[k].

Algorithm

  • Step 1: Iterate through the first n-2 elements using the outer loop

  • Step 2: For each element, iterate through elements from position i+1 to n-1

  • Step 3: If arr[i] > arr[j], iterate through remaining elements from j+1 to n-1

  • Step 4: Count elements where arr[j] > arr[k] to form valid inversions

  • Step 5: Return the total count

Example

Let's implement the brute force approach. In the array [10, 20, 5, 4, 50, 60, 30, 40], we can find inversions like (10, 5, 4) and (20, 5, 4).

<html>
<body>
   <h3>Using Brute Force Approach to Count Inversions of Size Three</h3>
   <div id="output"></div>
   <script>
      let output = document.getElementById('output');
      
      function countInversions(array) {
         let len = array.length;
         let count = 0;
         
         for (let i = 0; i < len - 2; i++) {
            for (let j = i + 1; j < len - 1; j++) {
               if (array[i] > array[j]) {
                  for (let k = j + 1; k < len; k++) {
                     if (array[j] > array[k]) {
                        count++;
                     }
                  }
               }
            }
         }
         return count;
      }
      
      let array = [10, 20, 5, 4, 50, 60, 30, 40];
      output.innerHTML = "Array: " + array + "<br>";
      output.innerHTML += "Count of inversions of size three: " + countInversions(array);
   </script>
</body>
</html>

Time and Space Complexity

  • Time Complexity: O(n³) due to three nested loops

  • Space Complexity: O(1) as we use constant extra space

Using Two Nested Loops (Optimized)

This approach optimizes the solution by treating each element as the middle element of the triplet. For each element, we count smaller elements on its right and larger elements on its left, then multiply these counts.

Algorithm

  • Step 1: Iterate through each element as the middle element

  • Step 2: Count larger elements on the left of current element

  • Step 3: Count smaller elements on the right of current element

  • Step 4: Multiply both counts and add to total inversions

Example

<html>
<body>
   <h3>Using Two Nested Loops to Count Inversions of Size Three</h3>
   <div id="output"></div>
   <script>
      let output = document.getElementById('output');
      
      function countInversionsOptimized(array) {
         let count = 0;
         let len = array.length;
         
         // Iterate through every element as middle element
         for (let j = 1; j < len - 1; j++) {
            
            // Count elements greater than arr[j] on the left
            let leftGreater = 0;
            for (let i = 0; i < j; i++) {
               if (array[i] > array[j]) {
                  leftGreater++;
               }
            }
            
            // Count elements smaller than arr[j] on the right
            let rightSmaller = 0;
            for (let k = j + 1; k < len; k++) {
               if (array[j] > array[k]) {
                  rightSmaller++;
               }
            }
            
            // Multiply counts to get inversions with arr[j] as middle element
            count += leftGreater * rightSmaller;
         }
         return count;
      }
      
      let array = [10, 20, 5, 4, 50, 60, 30, 40];
      output.innerHTML = "Array: " + array + "<br>";
      output.innerHTML += "Count of inversions of size three: " + countInversionsOptimized(array);
   </script>
</body>
</html>

Time and Space Complexity

  • Time Complexity: O(n²) due to two nested loops

  • Space Complexity: O(1) as we use constant extra space

Comparison

Approach Time Complexity Space Complexity Description
Brute Force O(n³) O(1) Check all possible triplets directly
Optimized O(n²) O(1) Count inversions by fixing middle element

Conclusion

We explored two methods to count inversions of size three: brute force with O(n³) complexity and an optimized approach with O(n²) complexity. The optimized method provides better performance by strategically counting elements around each middle element rather than checking all triplets explicitly.

Updated on: 2026-03-15T23:19:01+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements