Smallest greater elements in whole array | Efficient Python3 program to find smallest greater element in whole array for every element ; lowerbound function ; Driver code
def smallestGreater ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT newAr = [ ] NEW_LINE for p in s : NEW_LINE INDENT newAr . append ( p ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT temp = lowerBound ( newAr , 0 , len ( newAr ) , arr [ i ] ) NEW_LINE if ( temp < n ) : NEW_LINE INDENT print ( newAr [ temp ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " _ ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT def lowerBound ( vec , low , high , element ) : NEW_LINE INDENT array = [ 0 ] * ( len ( vec ) ) NEW_LINE k = 0 NEW_LINE for val in vec : NEW_LINE INDENT array [ k ] = val NEW_LINE k += 1 NEW_LINE DEDENT while ( low < high ) : NEW_LINE INDENT middle = low + int ( ( high - low ) / 2 ) NEW_LINE if ( element > array [ middle ] ) : NEW_LINE INDENT low = middle + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = middle NEW_LINE DEDENT DEDENT return low + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 ] NEW_LINE n = len ( ar ) NEW_LINE smallestGreater ( ar , n ) NEW_LINE DEDENT