Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code
def IsSorted ( A , B , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT x = max ( A [ i ] , B [ i ] ) ; NEW_LINE y = min ( A [ i ] , B [ i ] ) ; NEW_LINE A [ i ] = x ; NEW_LINE B [ i ] = y ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( A [ i ] <= A [ i - 1 ] or B [ i ] <= B [ i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 4 , 3 , 5 , 7 ] ; NEW_LINE B = [ 2 , 2 , 5 , 8 , 9 ] ; NEW_LINE n = len ( A ) ; NEW_LINE if ( IsSorted ( A , B , n ) ) : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT DEDENT