Count greater elements on the left side of every array element | C ++ program to implement the above approach ; Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void display ( int countLeftGreater [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << countLeftGreater [ i ] << " ▁ " ; } } void countGreater ( int arr [ ] , int N ) { set < int > St ; int countLeftGreater [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { St . insert ( arr [ i ] ) ; auto it = St . upper_bound ( arr [ i ] ) ; countLeftGreater [ i ] = distance ( it , St . end ( ) ) ; } display ( countLeftGreater , N ) ; } int main ( ) { int arr [ ] = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countGreater ( arr , N ) ; }