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, 3Input: arr[] = {3, 2, 1}
Output: {0, 1, 2}
Approach:
- Create a policy based data structure of type pair.
- Iterate the given array and perform the following steps –
- Apply order_of_key({X, N+1}) for each element X where N is the size of array.
Note: order_of_key is nothing but lower_bound. Also, we used N+1 because it is greater than all the indices in the array. - Let order_of_key comes out to be l, then the inversion count for current element will be equal to
which is ultimately the count of elements smaller than X and came before X in the array. - Insert the current element X along with its index in the policy-based data structure St. The index is inserted along with each element for its unique identification in the set and to deal with duplicates.
- Apply order_of_key({X, N+1}) for each element X where N is the size of array.
Below is the implementation of the above approach:
C++
// 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; } |
0 1 1 2 1 0 6
Time Complexity: ![]()
Recommended Posts:
- Static Data Structure vs Dynamic Data Structure
- Count inversion pairs in a matrix
- Remove duplicates from unsorted array using Map data structure
- Trie Data Structure using smart pointer and OOP in C++
- Remove duplicates from unsorted array using Set data structure
- Sort a 2D vector diagonally using Map Data Structure
- Sort numbers based on count of letters required to represent them in words
- Sort an array of strings based on count of distinct characters
- Maximum Count of pairs having equal Sum based on the given conditions
- Design an efficient data structure for given operations
- Data Structure for Dictionary and Spell Checker?
- Design a data structure that supports insert, delete, search and getRandom in constant time
- A data structure for n elements and O(1) operations
- Stack Data Structure (Introduction and Program)
- Ropes Data Structure (Fast String Concatenation)
- Advantages of Trie Data Structure
- Tango Tree Data Structure
- Dynamic Disjoint Set Data Structure for large range values
- Gap Buffer Data Structure
- Design a Queue data structure to get minimum or maximum in O(1) time
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.


