Floor of every element in same array | Prints greater elements on left side of every element ; Create a sorted copy of arr ; Traverse through arr [ ] and do binary search for every element . ; Floor of first element is - 1 if there is only one occurrence of it . ; Find the first element that is greater than or or equal to given element ; If next element is also same , then there are multiple occurrences , so print it ; Otherwise print previous element ; Driver Code
def printPrevGreater ( arr , n ) : NEW_LINE INDENT v = arr . copy ( ) NEW_LINE v . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == v [ 0 ] ) : NEW_LINE INDENT if ( arr [ i ] == v [ 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT continue NEW_LINE DEDENT if v . count ( arr [ i ] ) > 0 : NEW_LINE INDENT it = v [ v . index ( arr [ i ] ) ] NEW_LINE DEDENT else : NEW_LINE INDENT it = v [ n - 1 ] NEW_LINE DEDENT if ( it != v [ n - 1 ] and v [ v . index ( it ) + 1 ] == arr [ i ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( v [ v . index ( it ) - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 11 , 7 , 8 , 20 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE printPrevGreater ( arr , n ) NEW_LINE DEDENT