Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP Program for Program to cyclically rotate an array by one

PHP Program for Program to cyclically rotate an array by one

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}

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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[ ] = {1, 2, 3, 4, 4}
    • arr[3] = arr[2]
      • arr[ ] = {1, 2, 3, 3, 4}
    • arr[2] = arr[1]
      • arr[ ] = {1, 2, 2, 3, 4}
    • arr[1] = arr[0]
      • arr[ ] = {1, 1, 2, 3, 4}
  • Assign arr[0] = last_el
    • arr[0] = 5
      • arr[ ] = {5, 1, 2, 3, 4}
  • 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
?>


Output

Given 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 and 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 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] . " ";
}
 
?>


Output

Given 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] . " ";
}
 
?>


Output

Given 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!

RELATED ARTICLES

Most Popular

Recent Comments