PHP Program for Program to cyclically rotate an array by one
Last Updated :
18 Oct, 2023
Write a PHP program for a given array, the task is to cyclically rotate the array clockwise by one time.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: arr[] = {5, 1, 2, 3, 4}
Input: arr[] = {2, 3, 4, 5, 1}
Output: {1, 2, 3, 4, 5}
Approach:
Assign every element with its previous element and first element with the last element .
Illustrations:
Consider an array: arr[] = {1, 2, 3, 4, 5}
- Initialize last element in variable ‘last_el’ that is 5
- Then, iterate from n-1 to 1 and assign arr[i] = arr[i-1]
- arr[4] = arr[3]
- arr[3] = arr[2]
- arr[2] = arr[1]
- arr[1] = arr[0]
- Assign arr[0] = last_el
- Thus the required array will be {5, 1, 2, 3, 4}
Step-by-step approach:
- Store the last element in any temp variable
- Shift every element one position ahead
- Assign first value = last value (stored in temp variable).
Below is the implementation of the above approach:
PHP
<?php
// PHP code for program
// to cyclically rotate
// an array by one
function rotate(&$arr, $n)
{
$last_el = $arr[$n - 1];
for ($i = $n - 1;
$i > 0; $i--)
$arr[$i] = $arr[$i - 1];
$arr[0] = $last_el;
}
// 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
?>
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.
PHP Program for Program to cyclically rotate an array by one using Two Pointers Technique:
We can use two pointers, As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, we can do this by swapping every element with last element till we get to the last point.
Step-by-step approach:
- Take two pointers i and j which point to the first and last element of the array respectively.
- Start swapping arr[i] and arr[j] and keep j fixed and i moving towards j.
- Repeat the above step till i is not equal to j.
Below is the above approach of the above approach:
PHP
<?php
// PHP code for program
// to cyclically rotate
// an array by one
function rotate(&$arr, $n) {
$i = 0;
$j = $n - 1;
while ($i != $j) {
// Swap elements using a temporary variable
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
$i++;
}
}
// Driver code
$arr = array(1, 2, 3, 4, 5);
$n = count($arr);
// Function Call
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] . " ";
}
?>
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.
PHP Program for Program to cyclically rotate an array by one using Reversal Algorithm:
We can use Reversal Algorithm also , reverse first n-1 elements and then whole array which will result into one right rotation.
Step-by-step approach:
- Reverse the array two times.
- The first time we will reverse the first n-1(n=size of array) elements.
- Finally, we will get our rotated array by reversing the entire array.
Below is the implementation of the above approach:
PHP
<?php
// PHP code for program
// to cyclically rotate
// an array by one
function rotate(&$arr, $n, $k) {
// Reverse the first n-k elements
for ($i = 0, $j = $n - $k - 1; $i < $j; $i++, $j--) {
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
// Reverse the last k elements
for ($i = $n - $k, $j = $n - 1; $i < $j; $i++, $j--) {
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
// Reverse the entire array
for ($i = 0, $j = $n - 1; $i < $j; $i++, $j--) {
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
// Driver code
$arr = array(1, 2, 3, 4, 5);
$n = count($arr);
$k = 1; // Number of rotations
$i= $j=0;
echo "Given array is\n";
for ($i = 0; $i < $n; $i++) {
echo $arr[$i] . " ";
}
rotate($arr, $n, $k);
echo "\nRotated array is\n";
for ($i = 0; $i < $n; $i++) {
echo $arr[$i] . " ";
}
?>
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.
Please refer to the complete article on the Program to cyclically rotate an array by one for more details!
Similar Reads
PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena
3 min read
How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using
2 min read
How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
2 min read
PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr
3 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia
3 min read
PHP | DsVector rotate() Function The Ds\Vector::rotate() function is an inbuilt function in PHP which is used to rotate the array elements by a given number of rotation. Rotations also happen in-place. Syntax: void public Ds\Vector::rotate( $rotations ) Parameters: This function accepts single parameter $rotations which holds the n
2 min read
PHP array_flip() Function This built-in function of PHP is used to exchange elements within an array, i.e., exchange all keys with their associated values in an array and vice-versa. We must remember that the values of the array need to be valid keys, i.e. they need to be either integer or string. A warning will be thrown if
2 min read
PHP | DsSequence rotate() Function The Ds\Sequence::rotate() function is an inbuilt function in PHP which is used to rotate the sequence element by a given number of rotations. Syntax: void abstract public Ds\Sequence::rotate ( int $rotations ) Parameters: This function accepts single parameter $rotations which holds the number of ro
2 min read
PHP | DsDeque rotate() Function The Ds\Deque::rotate() function is an inbuilt function in PHP which is used to rotate the elements of Deque by the given number of rotations. Syntax: public Ds\Deque::rotate( $rotations ) : void Parameters: This function accepts single parameter $rotations which holds the number of rotation of the e
2 min read