PHP Program for Reversal algorithm for array rotation Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example : Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2Output : arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make an arrayThe Reversal Algorithm:Algorithm : rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n); reverse(arr[], 1, n);Let AB are the two parts of the input array where A = arr[0..d-1] and B = arr[d..n-1]. The idea of the algorithm is : Reverse A to get ArB, where Ar is the reverse of A.Reverse B to get ArBr, where Br is the reverse of B.Reverse all to get (ArBr) r = BA.Example: Let the array be arr[] = [1, 2, 3, 4, 5, 6, 7], d =2 and n = 7 A = [1, 2] and B = [3, 4, 5, 6, 7] Reverse A, we get ArB = [2, 1, 3, 4, 5, 6, 7]Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3]Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]Below is the implementation of the above approach : PHP <?php // PHP program for reversal // algorithm of array rotation /* Function to left rotate arr of size n by d */ function leftRotate(&$arr, $d, $n) { if ($d == 0) return; // in case the rotating factor is // greater than array length $d = ($d % $n); reverseArray($arr, 0, $d - 1); reverseArray($arr, $d, $n - 1); reverseArray($arr, 0, $n - 1); } /*Function to reverse $arr from index start to end*/ function reverseArray(&$arr, $start, $end) { while ($start < $end) { $temp = $arr[$start]; $arr[$start] = $arr[$end]; $arr[$end] = $temp; $start++; $end--; } } // Function to // print an array function printArray($arr, $size) { for ($i = 0; $i < $size; $i++) print $arr[$i]." "; } // Driver code $arr = array(1, 2, 3, 4, 5, 6, 7); $n = sizeof($arr); $d = 2; // Function calling leftRotate($arr, $d, $n); printArray($arr, $n); // This code is contributed // by ChitraNayal ?> Output3 4 5 6 7 1 2 Complexity Analysis:Time Complexity : O(n), where n represents the size of the given array.Auxiliary Space: O(1), no extra space is required, so it is a constant.Please refer complete article on Reversal algorithm for array rotation for more details! Comment More infoAdvertise with us K kartik Follow Improve Article Tags : PHP Codenation rotation Practice Tags : Codenation Similar Reads PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read Rotate an Array by d - Counterclockwise or Left Given an array of integers arr[] of size n, the task is to rotate the array elements to the left by d positions.Examples:Input: arr[] = {1, 2, 3, 4, 5, 6}, d = 2Output: {3, 4, 5, 6, 1, 2}Explanation: After first left rotation, arr[] becomes {2, 3, 4, 5, 6, 1} and after the second rotation, arr[] bec 15+ min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read Search in a Sorted and Rotated Array Given a sorted and rotated array arr[] of n distinct elements, the task is to find the index of given key in the array. If the key is not present in the array, return -1. Examples: Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 3], key = 3Output: 8Explanation: 3 is present at index 8 in arr[].Input: arr[] 15+ min read Check if Strings Are Rotations of Each Other Given two string s1 and s2 of same length, the task is to check whether s2 is a rotation of s1.Examples: Input: s1 = "abcd", s2 = "cdab"Output: trueExplanation: After 2 right rotations, s1 will become equal to s2.Input: s1 = "aab", s2 = "aba"Output: trueExplanation: After 1 left rotation, s1 will be 15+ min read Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co 7 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param 8 min read Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET 4 min read Like