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 times ; Starting position of array after k rotations in temp [ ] will be k % n ; Print array after k rotations ; Driver Code
< ? php function preprocess ( & $ arr , $ n , & $ temp ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ temp [ $ i ] = $ temp [ $ i + $ n ] = $ arr [ $ i ] ; } function leftRotate ( & $ arr , $ n , $ k , & $ temp ) { $ start = $ k % $ n ; for ( $ i = $ start ; $ i < $ start + $ n ; $ i ++ ) echo $ temp [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 1 , 3 , 5 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ temp [ 2 * $ n ] = array ( ) ; preprocess ( $ arr , $ n , $ temp ) ; $ k = 2 ; leftRotate ( $ arr , $ n , $ k , $ temp ) ; $ k = 3 ; leftRotate ( $ arr , $ n , $ k , $ temp ) ; $ k = 4 ; leftRotate ( $ arr , $ n , $ k , $ temp ) ; ? >