Modify given array to a non | Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating
function rotateArray ( arr , N ) { let v = arr ; v . sort ( ( a , b ) => a - b ) ; for ( let i = 1 ; i <= N ; ++ i ) { let x = arr [ N - 1 ] ; i = N - 1 ; while ( i -- ) { arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; } let isEqual = arr . every ( ( e , i ) => { return arr [ i ] == v [ i ] } ) if ( isEqual ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 3 , 4 , 5 , 1 , 2 ] ; let N = arr . length ; rotateArray ( arr , N ) ;