Reversal algorithm for right rotation of an array | Function to reverse arr [ ] from index start to end ; Function to right rotate arr [ ] of size n by d ; function to print an array ; driver code
function reverseArray ( arr , start , end ) { while ( start < end ) { let temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } return arr ; } function rightRotate ( arr , d , n ) { arr = reverseArray ( arr , 0 , n - 1 ) ; arr = reverseArray ( arr , 0 , d - 1 ) ; arr = reverseArray ( arr , d , n - 1 ) ; return arr ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = arr . length ; let k = 3 ; arr = rightRotate ( arr , k , n ) ; printArray ( arr , n ) ;