Python3 Program to Print all possible rotations of a given Array
Last Updated :
06 Sep, 2024
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:
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 :
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 =" ")
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:
- Define the Main class.
- Define the main method which takes args as input.
- Create the arr array and get its length n.
- Create the rotatedArr array with length 2*n and fill it with zeroes.
- Copy the arr array twice into the rotatedArr array.
- Loop over the n possible rotations.
- For each rotation, print a "[" character.
- Loop over the elements of the rotated array.
- Print each element followed by a space (if it's not the last element in the rotated array).
- After the loop, print a "]" character followed by a space.
- Run the main method if the script is being executed as the main module.
Below is the implementation of the above approach :
Python
class Main:
def main(args):
arr = [1, 2, 3, 4]
n = len(arr)
rotatedArr = [0] * (2*n)
# Copy the array twice into the rotatedArr
for i in range(n):
rotatedArr[i] = arr[i]
rotatedArr[i+n] = arr[i]
# 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="")
# Nikunj Sonigara
if __name__ == "__main__":
Main.main(None)
Output[1 2 3 4] [2 3 4 1] [3 4 1 2] [4 1 2 3]
Time Complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
Python3 Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31
2 min read
Python3 Program to Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes".Examples:Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation: After 2
3 min read
Python3 Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are
2 min read
Python Program to 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 to 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: Array[]
2 min read
Python3 Program to Find Mth element after K Right Rotations of an Array Python3 # Python3 program to implement # the above approach # Function to return Mth element of # array after k right rotations def getFirstElement(a, N, K, M): # The array comes to original state # after N rotations K %= N # If K is greater or equal to M if (K >= M): # Mth element after k right
1 min read