Sorting array with reverse around middle | Javascript program to find possibility to sort by multiple subarray reverse operation ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; driver code
function ifPossible ( arr , n ) { let copy = arr ; copy . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == copy [ i ] ) && ! ( arr [ n - 1 - i ] == copy [ i ] ) ) return false ; } return true ; } let arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ] ; let n = arr . length ; if ( ifPossible ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ; ;