# 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}
Explaination:
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[] = 
Output: 

Approach:
Follow the steps below to solve the problem:

1. Generate all possible rotations of the array, by performing a left rotation of the array one by one.
2. Print all possible rotations of the array until the same rotation of array is encountered.

Below is the implementation of the above approach :

## 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``;`   `// 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 = 1;` `  ``arr = 2;` `  ``arr = 3;` `  ``arr = 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`

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)

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.

My Personal Notes arrow_drop_up 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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : gk74533, Rajput-Ji

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.