PHP Program for Program to cyclically rotate an array by one
Given an array, cyclically rotate the array clockwise by one.
Examples:
Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4}
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
PHP
<?php // PHP code for program // to cyclically rotate // an array by one function rotate(& $arr , $n ) { $x = $arr [ $n - 1]; for ( $i = $n - 1; $i > 0; $i --) $arr [ $i ] = $arr [ $i - 1]; $arr [0] = $x ; } // Driver code $arr = array (1, 2, 3, 4, 5); $n = sizeof( $arr ); echo "Given array is \n" ; for ( $i = 0; $i < $n ; $i ++) echo $arr [ $i ] . " " ; rotate( $arr , $n ); echo "\nRotated array is\n" ; for ( $i = 0; $i < $n ; $i ++) echo $arr [ $i ] . " " ; // This code is contributed // by ChitraNayal ?> |
Please refer complete article on Program to cyclically rotate an array by one for more details!
Please Login to comment...