C++ Program to Print all possible rotations of a given Array
Last Updated :
19 May, 2023
Given an integer array arr[] of size N, the task is to print all possible rotations of the array.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1}
Explanation:
Initial arr[] = {1, 2, 3, 4}
After first rotation arr[] = {4, 1, 2, 3}
After second rotation arr[] = {3, 4, 1, 2}
After third rotation arr[] = {2, 3, 4, 1}
After fourth rotation, arr[] returns to its original form.Input: arr[] = [1]
Output: [1]
Approach 1:
Follow the steps below to solve the problem:
- Generate all possible rotations of the array, by performing a left rotation of the array one by one.
- Print all possible rotations of the array until the same rotation of array is encountered.
Below is the implementation of the above approach :
C++
// C++ program to print
// all possible rotations
// of the given array
#include <iostream>
using namespace std;
// Global declaration of array
int arr[10000];
// Function to reverse array
// between indices s and e
void reverse(int arr[],
int s, int e)
{
while(s < e)
{
int tem = arr[s];
arr[s] = arr[e];
arr[e] = tem;
s = s + 1;
e = e - 1;
}
}
// Function to generate all
// possible rotations of array
void fun(int arr[], int k)
{
int n = 4 - 1;
int v = n - k;
if (v >= 0)
{
reverse(arr, 0, v);
reverse(arr, v + 1, n);
reverse(arr, 0, n);
}
}
// Driver code
int main()
{
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for(int i = 0; i < 4; i++)
{
fun(arr, i);
cout << ("[");
for(int j = 0; j < 4; j++)
{
cout << (arr[j]) << ", ";
}
cout << ("]");
}
}
// This code is contributed by Princi Singh
Output: [1, 2, 3, 4] [4, 1, 2, 3] [2, 3, 4, 1] [3, 4, 1, 2]
Time Complexity: O (N2)
Auxiliary Space: O (1)
Approach 2: Follow the steps below to solve the problem:
- Declare an array arr and initialize it with some values. Find the length of the array n.
- Create a new array rotatedArr of twice the length of the input array. Copy the elements of the input array twice into the rotatedArr, first in the first half of the array and then in the second half of the array.
- Iterate over the indices from 0 to n and generate all possible rotations of the array. For each index i, print a sub-array of rotatedArr starting from index i and having length n.
C++
#include <iostream>
using namespace std;
// Nikunj Sonigara
int main()
{
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
int rotatedArr[2*n];
// Copy the array twice into the rotatedArr
for (int i = 0; i < n; i++) {
rotatedArr[i] = arr[i];
rotatedArr[i+n] = arr[i];
}
// Generate all possible rotations
for (int i = 0; i < n; i++) {
cout << "[";
for (int j = i; j < i+n; j++) {
cout << rotatedArr[j];
if(j != i+n-1)
cout << " ";
}
cout << "] ";
}
return 0;
}
Output[1 2 3 4] [2 3 4 1] [3 4 1 2] [4 1 2 3]
Time Complexity: O(n2)
Space Complexity: O(n)
Please refer complete article on Print all possible rotations of a given Array for more details!
Similar Reads
Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
8 min read
Print left rotation of array in O(n) time and O(1) space Given an array of size n and multiple values around which we need to left rotate the array. How to quickly print multiple left rotations?Examples : Input : arr[] = {1, 3, 5, 7, 9}k1 = 1k2 = 3k3 = 4k4 = 6Output : 3 5 7 9 17 9 1 3 59 1 3 5 73 5 7 9 1Input : arr[] = {1, 3, 5, 7, 9}k1 = 14 Output : 9 1
15+ min read
Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a
8 min read
Maximum sum of i*arr[i] among all rotations of a given array Given an integer array arr[] of size n, find the maximum value of the expression i * arr[i] (for all i from 0 to n-1) after rotating the array any number of times.Examples: Input: arr[] = [8, 3, 1, 2]Output: 29Explanation: Out of all the possible configurations by rotating the elements: arr[] = [3,
12 min read
Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So
11 min read
Count of possible rotations of given Array to remove largest element from first half Given an array arr[ ] with even length N, the task is to find the number of cyclic shifts (rotations) possible for this array, such that the first half of array does not contain the maximum element. Examples: Input: N = 6, arr[ ] = { 3, 3, 5, 3, 3, 3 }Output: 3Explanation: The maximum element here i
6 min read