Permutation of an array that has smaller values from another array | Function to print required permutation ; Storing elements and indexes ; Filling the answer array ; pair element of A and B ; Fill the remaining elements of answer ; Output required permutation ; Driver Code
def anyPermutation ( A , B , n ) : NEW_LINE INDENT Ap , Bp = [ ] , [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Ap . append ( [ A [ i ] , i ] ) NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT Bp . append ( [ B [ i ] , i ] ) NEW_LINE DEDENT Ap . sort ( ) NEW_LINE Bp . sort ( ) NEW_LINE i , j = 0 , 0 , NEW_LINE ans = [ 0 ] * n NEW_LINE remain = [ ] NEW_LINE while i < n and j < n : NEW_LINE INDENT if Ap [ i ] [ 0 ] > Bp [ j ] [ 0 ] : NEW_LINE INDENT ans [ Bp [ j ] [ 1 ] ] = Ap [ i ] [ 0 ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT remain . append ( i ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ans [ i ] == 0 : NEW_LINE INDENT ans [ i ] = Ap [ remain [ j ] ] [ 0 ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 12 , 24 , 8 , 32 ] NEW_LINE B = [ 13 , 25 , 32 , 11 ] NEW_LINE n = len ( A ) NEW_LINE anyPermutation ( A , B , n ) NEW_LINE DEDENT