Count of greater elements for each element in the Array | C ++ implementation of the above approach ; Store the frequency of the array elements ; Store the sum of frequency of elements greater than the current element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOfGreaterElements ( int arr [ ] , int n ) { map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } int x = 0 ; for ( auto it = mp . rbegin ( ) ; it != mp . rend ( ) ; it ++ ) { int temp = it -> second ; mp [ it -> first ] = x ; x += temp ; } for ( int i = 0 ; i < n ; i ++ ) cout << mp [ arr [ i ] ] << " ▁ " ; } int main ( ) { int arr [ ] = { 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countOfGreaterElements ( arr , n ) ; return 0 ; }