Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | C ++ implementation of above approach ; 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
#include <iostream> NEW_LINE using namespace std ; bool IsSorted ( int A [ ] , int B [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = max ( A [ i ] , B [ i ] ) ; y = min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } int main ( ) { int A [ ] = { 1 , 4 , 3 , 5 , 7 } ; int B [ ] = { 2 , 2 , 5 , 8 , 9 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << ( IsSorted ( A , B , n ) ? " True " : " False " ) ; return 0 ; }