Ceiling of every element in same array | Python implementation of efficient algorithm to find floor of every element ; 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 ; Find the location of first element that is greater than the given element ; Since arr [ i ] also exists in array , v [ it - 1 ] will be same as arr [ i ] . Let us check v [ it - 2 ] is also same as arr [ i ] . If true , then arr [ i ] exists twice in array , so ceiling is same same as arr [ i ] ; If next element is also same , then there are multiple occurrences , so print it ; Driver code
import bisect NEW_LINE def printPrevGreater ( arr , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT v = list ( arr ) NEW_LINE v . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT it = bisect . bisect_right ( v , arr [ i ] ) NEW_LINE if ( it - 1 ) != 0 and v [ it - 2 ] == arr [ i ] : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT elif it <= n - 1 : NEW_LINE INDENT print ( v [ it ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 11 , 10 , 20 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE printPrevGreater ( arr , n ) NEW_LINE DEDENT