Check if two arrays can be made equal by reversing any subarray once | Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
function checkArray ( A , B , N ) { var start = 0 ; var end = N - 1 ; for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { start = i ; break ; } } for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { end = i ; break ; } } var l = start ; var r = end ; var d = parseInt ( ( r - l + 2 ) / 2 ) ; for ( var i = 0 ; i < d ; i ++ ) { var t = A [ l + i ] ; A [ l + i ] = A [ r - i ] ; A [ r - i ] = t ; } for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var A = [ 1 , 3 , 2 , 4 ] ; var B = [ 1 , 2 , 3 , 4 ] ; var N = A . length ; checkArray ( A , B , N ) ;