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
def display ( countLeftGreater , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( countLeftGreater [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def countGreater ( arr , N ) : NEW_LINE INDENT St = set ( ) NEW_LINE countLeftGreater = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT St . add ( arr [ i ] ) NEW_LINE it = 0 NEW_LINE for st in St : NEW_LINE INDENT if ( arr [ i ] < st ) : NEW_LINE INDENT break NEW_LINE DEDENT it += 1 NEW_LINE DEDENT countLeftGreater [ i ] = abs ( it - len ( St ) ) NEW_LINE DEDENT display ( countLeftGreater , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 1 , 2 , 3 , 0 , 11 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE countGreater ( arr , N ) NEW_LINE DEDENT