Inversion Count using Policy Based Data Structure

Pre-requisite: Policy based data structure

Given an array arr[], the task is to find the number of inversions for each element of the array.

Inversion Count: for an array indicates – how far (or close) the array is from being sorted. If the array is already sorted then the inversion count is 0. If the array is sorted in the reverse order then the inversion count is the maximum.

Formally, Number of indices and such that and .

Examples:



Input: {5, 2, 3, 2, 3, 8, 1}
Output: {0, 1, 1, 2, 1, 0, 6}
Explanation:
Inversion count for each elements –
Element at index 0: There are no elements with less index than 0, which is greater than arr[0].
Element at index 1: There is one element with less index than 1, which is greater than 2. That is 5.
Element at index 2: There is one element with less index than 2, which is greater than 3. That is 5.
Element at index 3: There are two elements with less index than 3, which is greater than 2. That is 5, 3.
Element at index 4: There is one element with less index than 4, which is greater than 3. That is 5.
Element at index 5: There are no elements with less index than 5, which is greater than 8.
Element at index 6: There are six elements with less index than 6, which is greater than 1. That is 5, 2, 3, 2, 3

Input: arr[] = {3, 2, 1}
Output: {0, 1, 2}

Approach:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// Inversion Count using Policy
// Based Data Structure
  
#include <bits/stdc++.h>
  
// Header files for policy based
// data structure which are
// to be included
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
  
using namespace __gnu_pbds;
using namespace std;
  
typedef tree<pair<int, int>, null_type,
             less<pair<int, int> >, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
  
// Function to find the inversion count
// of the elements of the array
void inversionCount(int arr[], int n)
{
    int ans[n];
  
    // Making a new policy based data
    // structure which will
    // store pair<int, int>
    new_data_set St;
  
    // Loop to iterate over the elements
    // of the array
    for (int i = 0; i < n; i++) {
  
        // Now to find lower_bound of
        // the element X, we will use pair
        // as {x, n+1} to cover all the
        // elements and even the duplicates
        int cur = St.order_of_key({ arr[i],
                                    n + 1 });
  
        ans[i] = St.size() - cur;
  
        // Store element along with index
        St.insert({ arr[i], i });
    }
  
    for (int i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";
}
  
// Driver Code
int main()
{
    int arr[] = { 5, 2, 3, 2, 3, 8, 1 };
    int n = sizeof(arr) / sizeof(int);
  
    // Function Call
    inversionCount(arr, n);
  
    return 0;
}
chevron_right

Output:
0 1 1 2 1 0 6

Time Complexity:





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :