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 ( strictlyincreasing ) 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
function IsSorted ( A , B , n ) { for ( var i = 0 ; i < n ; i ++ ) { var x , y ; x = Math . max ( A [ i ] , B [ i ] ) ; y = Math . min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( var i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } var A = [ 1 , 4 , 3 , 5 , 7 ] ; var B = [ 2 , 2 , 5 , 8 , 9 ] ; var n = A . length ; document . write ( IsSorted ( A , B , n ) ? " " : " " ) ;