Closest greater element for every array element from another array | Python implementation to find result from target array for closest element ; Function for printing resultant array ; sort list for ease ; list for result ; calculate resultant array ; check location of upper bound element ; if no element found push - 1 ; else puch the element ; add to resultant ; Driver code
import bisect NEW_LINE def closestResult ( a , b , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE c = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT up = bisect . bisect_right ( a , b [ i ] ) NEW_LINE if up == n : NEW_LINE INDENT c . append ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT c . append ( a [ up ] ) NEW_LINE DEDENT DEDENT print ( " Result ▁ = ▁ " , end = " " ) NEW_LINE for i in c : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 5 , 6 , 1 , 8 , 9 ] NEW_LINE b = [ 2 , 1 , 0 , 5 , 4 , 9 ] NEW_LINE n = len ( a ) NEW_LINE closestResult ( a , b , n ) NEW_LINE DEDENT