Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort the array , else return false . ; Copying the array . ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driver Code
< ? php function checkReverse ( $ arr , $ n ) { $ temp [ $ n ] = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ temp [ $ i ] = $ arr [ $ i ] ; sort ( $ temp , 0 ) ; $ front ; for ( $ front = 0 ; $ front < $ n ; $ front ++ ) if ( $ temp [ $ front ] != $ arr [ $ front ] ) break ; $ back ; for ( $ back = $ n - 1 ; $ back >= 0 ; $ back -- ) if ( $ temp [ $ back ] != $ arr [ $ back ] ) break ; if ( $ front >= $ back ) return true ; do { $ front ++ ; if ( $ arr [ $ front - 1 ] < $ arr [ $ front ] ) return false ; } while ( $ front != $ back ) ; return true ; } $ arr = array ( 1 , 2 , 5 , 4 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( checkReverse ( $ arr , $ n ) ) echo " Yes " . " STRNEWLINE " ; else echo " No " . " STRNEWLINE " ; ? >