Count greater elements on the left side of every array element | 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
function display ( countLeftGreater , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( countLeftGreater [ i ] , " " ) ; } } function countGreater ( arr , N ) { let St = new Set ( ) ; let countLeftGreater = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { St . add ( arr [ i ] ) ; let it = 0 ; for ( let st of St ) { if ( arr [ i ] < st ) it += 1 ; } countLeftGreater [ i ] = Math . abs ( it ) ; } display ( countLeftGreater , N ) ; } let arr = [ 12 , 1 , 2 , 3 , 0 , 11 , 4 ] ; let N = arr . length ; countGreater ( arr , N ) ;