Print all possible rotations of a given Array
Last Updated :
01 Aug, 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
Java
// Java program to print
// all possible rotations
// of the given array
class GFG{
// Global declaration of array
static int arr[] = new int[10000];
// Function to reverse array
// between indices s and e
public static 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
public static 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
public static void main(String args[])
{
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for(int i = 0; i < 4; i++)
{
fun(arr, i);
System.out.print("[");
for(int j = 0; j < 4; j++)
{
System.out.print(arr[j] + ", ");
}
System.out.print("]");
}
}
}
// This code is contributed by gk74533
Python
# Python program to print
# all possible rotations
# of the given array
# Function to reverse array
# between indices s and e
def reverse(arr, s, e):
while s < e:
tem = arr[s]
arr[s] = arr[e]
arr[e] = tem
s = s + 1
e = e - 1
# Function to generate all
# possible rotations of array
def fun(arr, k):
n = len(arr)-1
# k = k % n
v = n - k
if v>= 0:
reverse(arr, 0, v)
reverse(arr, v + 1, n)
reverse(arr, 0, n)
return arr
# Driver Code
arr = [1, 2, 3, 4]
for i in range(0, len(arr)):
count = 0
p = fun(arr, i)
print(p, end =" ")
C#
// C# program to print
// all possible rotations
// of the given array
using System;
class GFG{
// Global declaration of array
static int []arr = new int[10000];
// Function to reverse array
// between indices s and e
public static 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
public static 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
public static void Main(String []args)
{
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for(int i = 0; i < 4; i++)
{
fun(arr, i);
Console.Write("[");
for(int j = 0; j < 4; j++)
{
Console.Write(arr[j] + ", ");
}
Console.Write("]");
}
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript program to print
// all possible rotations
// of the given array
// Global declaration of array
arr = Array.from({length: 10000}, (_, i) => 0);
// Function to reverse array
// between indices s and e
function reverse(arr, s , e)
{
while(s < e)
{
var tem = arr[s];
arr[s] = arr[e];
arr[e] = tem;
s = s + 1;
e = e - 1;
}
}
// Function to generate all
// possible rotations of array
function fun(arr , k)
{
var n = 4 - 1;
var v = n - k;
if (v >= 0)
{
reverse(arr, 0, v);
reverse(arr, v + 1, n);
reverse(arr, 0, n);
}
}
// Driver code
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for(i = 0; i < 4; i++)
{
fun(arr, i);
document.write("[");
for(j = 0; j < 4; j++)
{
document.write(arr[j] + ", ");
}
document.write("]<br>");
}
// This code is contributed by 29AjayKumar
</script>
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), since no extra space has been taken.
Approach 2: Follow the steps below to solve the problem:
- Initialize the original array arr and its size n.
- Create a new array rotatedArr of size 2 * n and copy the elements of arr into it.
- Generate all possible rotations by iterating over i from 0 to n-1 and printing the bracketed sequence of elements from rotatedArr[i] to rotatedArr[i + n - 1].
- Exit the program.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
int main()
{
std::vector<int> arr = {1, 2, 3, 4};
int n = arr.size();
std::vector<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];
}
// Nikunj Sonigara
// Generate all possible rotations
for (int i = 0; i < n; i++) {
std::cout << "[";
for (int j = i; j < i + n; j++) {
std::cout << rotatedArr[j];
if (j != i + n - 1)
std::cout << " ";
}
std::cout << "] ";
}
return 0;
}
Java
public class GFG {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = arr.length;
int[] rotatedArr = new int[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];
}
// Nikunj Sonigara
// Generate all possible rotations
for (int i = 0; i < n; i++) {
System.out.print("[");
for (int j = i; j < i+n; j++) {
System.out.print(rotatedArr[j]);
if(j != i+n-1)
System.out.print(" ");
}
System.out.print("] ");
}
}
}
Python3
arr = [1, 2, 3, 4]
n = len(arr)
rotatedArr = arr + arr
# Nikunj Sonigara
# Generate all possible rotations
for i in range(n):
print("[", end="")
for j in range(i, i + n):
print(rotatedArr[j], end="")
if j != i + n - 1:
print(" ", end="")
print("]", end=" ")
C#
using System;
class GFG {
static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4 };
int n = arr.Length;
int[] rotatedArr = new int[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++) {
Console.Write("[");
for (int j = i; j < i + n; j++) {
Console.Write(rotatedArr[j]);
if (j != i + n - 1)
Console.Write(" ");
}
Console.Write("] ");
}
}
}
JavaScript
function rotateArray(arr) {
const n = arr.length;
const rotatedArr = new Array(2 * n);
// Copy the array twice into the rotatedArr
for (let i = 0; i < n; i++) {
rotatedArr[i] = arr[i];
rotatedArr[i + n] = arr[i];
}
// Generate all possible rotations and store them in an array
const allRotations = [];
for (let i = 0; i < n; i++) {
let rotation = [];
for (let j = i; j < i + n; j++) {
rotation.push(rotatedArr[j]);
}
allRotations.push(rotation);
}
return allRotations;
}
// Test the function
const arr = [1, 2, 3, 4];
const result = rotateArray(arr);
// Print the result
result.forEach(rotation => {
console.log('[ ' + rotation.join(' ') + ' ]');
});
Output[1 2 3 4] [2 3 4 1] [3 4 1 2] [4 1 2 3]
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
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
Generate all rotations of a given string Given a string S. The task is to print all the possible rotated strings of the given string.Examples: Input : S = "geeks"Output : geeks eeksg eksge ksgee sgeekInput : S = "abc" Output : abc bca cabMethod 1 (Simple): The idea is to run a loop from i = 0 to n - 1 ( n = length of string) i.e for each p
8 min read
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
11 min read
Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3 Output: 8 9 10 1 2 3 4 5 6 7 Input: arr[] = {121, 232, 33, 43 ,5} k = 2 Output: 43 5 121 232 33 Note : In the below solution, k is assumed to be smaller than or equal to n
6 min read
Quickly find multiple left rotations of an array | Set 1 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 = 6Output: 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:
15 min read
Rotate an Array - Clockwise or Right Rotations in the array is defined as the process of rearranging the elements in an array by shifting each element to a new position. This is mostly done by rotating the elements of the array clockwise or counterclockwise.Table of ContentTypes of Rotations in ArrayHow to implement rotations in an arr
15+ min read
Print array after it is right rotated K times Given an Array of size N and a value K, around which we need to right rotate the array. How do you quickly print the right rotated array?Examples : Input: Array[] = {1, 3, 5, 7, 9}, K = 2.Output: 7 9 1 3 5Explanation:After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5} Input: Arr
15+ min read
Juggling Algorithm for Array Rotation Given an array arr[], the task is to rotate the array by d positions to the left using juggling algorithm. 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[] becomes
9 min read
Rotation Count in a Rotated Sorted array Given an array arr[] having distinct numbers sorted in increasing order and the array has been right rotated (i.e, the last element will be cyclically shifted to the starting position of the array) k number of times, the task is to find the value of k.Examples: Input: arr[] = {15, 18, 2, 3, 6, 12}Ou
12 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