Count rotations required to sort given array in non | Function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; Stores count of arr [ i + 1 ] > arr [ i ] ; Store last index of arr [ i + 1 ] > arr [ i ] ; Traverse the given array ; If the adjacent elements are in increasing order ; Increment count ; Update index ; Print result according to the following conditions ; Otherwise , it is not possible to sort the array ; Given array ; Function Call
function minMovesToSort ( arr , N ) { let count = 0 ; let index = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { document . write ( " " ) ; } else if ( count == N - 1 ) { document . write ( N - 1 ) ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { document . write ( index + 1 ) ; } else { document . write ( " " ) ; } } let arr = [ 2 , 1 , 5 , 4 , 2 ] ; let N = arr . length ; minMovesToSort ( arr , N ) ;