Given an array of size n and multiple values around which we need to left rotate the array. How to quickly find multiple left rotations?
Examples:
Input : arr[] = {1, 3, 5, 7, 9}
k1 = 1
k2 = 3
k3 = 4
k4 = 6
Output : 3 5 7 9 1
7 9 1 3 5
9 1 3 5 7
3 5 7 9 1
Input : arr[] = {1, 3, 5, 7, 9}
k1 = 14
Output : 9 1 3 5 7
Simple Approach: We have already discussed different approaches in below posts.
- Left Rotation of array (Simple and Juggling Algorithms).
- Block swap algorithm for array rotation
- Reversal algorithm for array rotation
The best of above approaches take O(n) time and O(1) extra space.
Efficient Approach:
The above approaches work well when there is single rotation required. The approaches also modify the original array. To handle multiple queries of array rotation, we use a temp array of size 2n and quickly handle rotations.
Step 1 : Copy the entire array two times in temp[0..2n-1] array.
Step 2 : Starting position of array after k rotations in temp[] will be k % n. We do k
Step 3 : Print temp[] array from k % n to k % n + n.
C++
// CPP implementation of left rotation of // an array K number of times #include<bits/stdc++.h> using namespace std; // Fills temp[] with two copies of arr[] void preprocess(int arr[], int n, int temp[]) { // Store arr[] elements at i and i + n for (int i = 0; i<n; i++) temp[i] = temp[i + n] = arr[i]; } // Function to left rotate an array k times void leftRotate(int arr[], int n, int k, int temp[]) { // Starting position of array after k // rotations in temp[] will be k % n int start = k % n; // Print array after k rotations for (int i = start; i < start + n; i++) cout << temp[i] << " "; cout << endl; } // Driver program int main() { int arr[] = {1, 3, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); int temp[2*n]; preprocess(arr, n, temp); int k = 2; leftRotate(arr, n, k, temp); k = 3; leftRotate(arr, n, k, temp); k = 4; leftRotate(arr, n, k, temp); return 0; } |
Java
// Java implementation of left rotation of // an array K number of times class LeftRotate { // Fills temp[] with two copies of arr[] static void preprocess(int arr[], int n, int temp[]) { // Store arr[] elements at i and i + n for (int i = 0; i<n; i++) temp[i] = temp[i + n] = arr[i]; } // Function to left rotate an array k time static void leftRotate(int arr[], int n, int k, int temp[]) { // Starting position of array after k // rotations in temp[] will be k % n int start = k % n; // Print array after k rotations for (int i = start; i < start + n; i++) System.out.print(temp[i] + " "); System.out.print("\n"); } // Driver program public static void main (String[] args) { int arr[] = {1, 3, 5, 7, 9}; int n = arr.length; int temp[] = new int[2*n]; preprocess(arr, n, temp); int k = 2; leftRotate(arr, n, k, temp); k = 3; leftRotate(arr, n, k, temp); k = 4; leftRotate(arr, n, k, temp); } } /*This code is contributed by Prakriti Gupta*/ |
Python3
# Python3 implementation of left rotation # of an array K number of times # Fills temp with two copies of arr def preprocess(arr, n): temp = [None] * (2 * n) # Store arr elements at i and i + n for i in range(n): temp[i] = temp[i + n] = arr[i] return temp # Function to left rotate an array k times def leftRotate(arr, n, k, temp): # Starting position of array after k # rotations in temp will be k % n start = k % n # Print array after k rotations for i in range(start, start + n): print(temp[i], end = " ") print("") # Driver program arr = [1, 3, 5, 7, 9] n = len(arr) temp = preprocess(arr, n) k = 2leftRotate(arr, n, k, temp) k = 3leftRotate(arr, n, k, temp) k = 4leftRotate(arr, n, k, temp) # This code is contributed by Sanghamitra Mishra |
C#
// Java implementation of left rotation of // an array K number of times using System; class LeftRotate { // Fills temp[] with two copies of arr[] static void preprocess(int []arr, int n, int[] temp) { // Store arr[] elements at i and i + n for (int i = 0; i<n; i++) temp[i] = temp[i + n] = arr[i]; } // Function to left rotate an array k time static void leftRotate(int []arr, int n, int k, int []temp) { // Starting position of array after k // rotations in temp[] will be k % n int start = k % n; // Print array after k rotations for (int i = start; i < start + n; i++) Console.Write(temp[i] + " "); Console.WriteLine(); } // Driver program public static void Main () { int []arr = {1, 3, 5, 7, 9}; int n = arr.Length; int []temp = new int[2*n]; preprocess(arr, n, temp); int k = 2; leftRotate(arr, n, k, temp); k = 3; leftRotate(arr, n, k, temp); k = 4; leftRotate(arr, n, k, temp); } } //This code is contributed by vt_m. |
PHP
<?php // PHP implementation of // left rotation of an // array K number of times // Fills $temp with // two copies of $arr function preprocess(&$arr, $n, &$temp) { // Store $arr elements // at i and i + n for ($i = 0; $i < $n; $i++) $temp[$i] = $temp[$i + $n] = $arr[$i]; } // Function to left rotate // an array k times function leftRotate(&$arr, $n, $k, &$temp) { // Starting position of // array after k rotations // in temp[] will be k % n $start = $k % $n; // Print array after // k rotations for ($i = $start; $i < $start + $n; $i++) echo $temp[$i] . " "; echo "\n"; } // Driver Code $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); // This code is contributed // by ChitraNayal ?> |
Output:
5 7 9 1 3 7 9 1 3 5 9 1 3 5 7
Note that the task to find starting address of rotation takes O(1) time. It is printing the elements that takes O(n) time.
Space optimized Approach : The above method takes extra space. Below given is the space optimized solution. Thanks to frenzy77 for suggesting this approach.
C++
// CPP implementation of left rotation of // an array K number of times #include<bits/stdc++.h> using namespace std; // Function to left rotate an array k times void leftRotate(int arr[], int n, int k) { // Print array after k rotations for (int i = k; i < k + n; i++) cout << arr[i%n] << " "; } // Driver program int main() { int arr[] = {1, 3, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; leftRotate(arr, n, k); cout << endl; k = 3; leftRotate(arr, n, k); cout << endl; k = 4; leftRotate(arr, n, k); cout << endl; return 0; } |
Java
// Java implementation of // left rotation of an // array K number of times import java.io.*; class GFG { // Function to left rotate // an array k times static void leftRotate(int arr[], int n, int k) { // Print array after // k rotations for (int i = k; i < k + n; i++) System.out.print(arr[i % n] + " "); } // Driver Code public static void main (String[] args) { int arr[] = {1, 3, 5, 7, 9}; int n = arr.length; int k = 2; leftRotate(arr, n, k); System.out.println(); k = 3; leftRotate(arr, n, k); System.out.println(); k = 4; leftRotate(arr, n, k); System.out.println(); } } // This code is contributed by ajit |
Python 3
# Python3 implementation of # left rotation of an array # K number of times # Function to left rotate # an array k times def leftRotate(arr, n, k): # Print array # after k rotations for i in range(k, k + n): print(str(arr[i % n]), end = " ") # Driver Code arr = [1, 3, 5, 7, 9] n = len(arr) k = 2; leftRotate(arr, n, k) print() k = 3; leftRotate(arr, n, k) print() k = 4leftRotate(arr, n, k) print() # This code is contributed # by ChitraNayal |
C#
// C# implementation of // left rotation of an // array K number of times using System; class GFG { // Function to left rotate // an array k times static void leftRotate(int []arr, int n, int k) { // Print array after // k rotations for (int i = k; i < k + n; i++) Console.Write(arr[i % n] + " "); } // Driver Code static public void Main () { int []arr = {1, 3, 5, 7, 9}; int n = arr.Length; int k = 2; leftRotate(arr, n, k); Console.WriteLine(); k = 3; leftRotate(arr, n, k); Console.WriteLine(); k = 4; leftRotate(arr, n, k); Console.WriteLine(); } } // This code is contributed // by akt_mit |
PHP
<?php // PHP implementation of left rotation of // an array K number of times // Function to left rotate an array k times function leftRotate($arr, $n, $k) { // Print array after k rotations for ($i = $k; $i < $k + $n; $i++) echo $arr[$i % $n] ," "; } // Driver program $arr = array (1, 3, 5, 7, 9); $n = sizeof($arr); $k = 2; leftRotate($arr, $n, $k); echo "\n"; $k = 3; leftRotate($arr, $n, $k); echo "\n"; $k = 4; leftRotate($arr, $n, $k); echo "\n"; // This code is contributed by aj_36 ?> |
Output:
5 7 9 1 3 7 9 1 3 5 9 1 3 5 7
This article is contributed by nuclode and Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the Mth element of the Array after K left rotations
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Find element at given index after a number of rotations
- Check if strings are rotations of each other or not | Set 2
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Maximum sum of i*arr[i] among all rotations of a given array
- Range sum queries for anticlockwise rotations of Array by K indices
- Count of rotations required to generate a sorted array
- Print all possible rotations of a given Array
- Mth element after K Right Rotations of an Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find an element in array such that sum of left array is equal to sum of right array
- A Program to check if strings are rotations of each other or not
- Check if all rows of a matrix are circular rotations of each other
- Count rotations divisible by 4
- Check if two numbers are bit rotations of each other or not
- Count rotations in sorted and rotated linked list
- Count rotations divisible by 8
- Generating numbers that are divisor of their right-rotations

