Quickly find multiple left rotations of an array | Set 1 | Fills temp with two copies of arr ; Store arr elements at i and i + n ; Function to left rotate an array k time ; Starting position of array after k rotations in temp will be k % n ; Print array after k rotations ; Driver program
function preprocess ( arr , n , temp ) { for ( i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } function leftRotate ( arr , n , k , temp ) { var start = k % n ; for ( i = start ; i < start + n ; i ++ ) document . write ( temp [ i ] + " " ) ; document . write ( " " ) ; } var arr = [ 1 , 3 , 5 , 7 , 9 ] ; var n = arr . length ; var temp = Array ( 2 * n ) . fill ( 0 ) ; preprocess ( arr , n , temp ) ; var k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ;