Python Program to Rotate an Array

Array Rotation

Python program for array rotation has been shown here. An array rotation is described as the rotation of array elements to either left or right by k positions.


Example 1: Left rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the left.

=>A[] = {20, 30, 40, 50, 60, 10}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {30, 40, 50, 60, 10, 20}

So, array A[] after left rotation by 2 positions: {30, 40, 50, 60, 10, 20}



Example 2: Right rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the right.

=>A[] = {60, 10, 20, 30, 40, 50}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {50, 60, 10, 20, 30, 40}

So, array A[] after right rotation by 2 positions: {50, 60, 10, 20, 30, 40}






1. Python program & output to Rotate an Array

Code has been copied
# ***********************************
#         alphabetacoder.com
# Python program to rotate an array
# ***********************************

# function to left shift the array
def leftShift(arr, s):
    # take input of the number of position of rotation
    k = int(input("Enter the number of position to be left shifted: "))
    # left shift the array upto k position
    for i in range(0, k):
        # get the first elements
        e = arr[0]
        # shift the elements
        for j in range(0, s - 1):
            arr[j] = arr[j + 1]

        arr[s - 1] = e

    # display the finalarray
    print("Output array: ", arr)


# function to right shift the array
def rightShift(arr, s):
    # take input of the number of position of rotation
    k = int(input("Enter the number of position to be right shifted: "))
    # right shift the array upto k position
    for i in range(0, k):
        # get the last elements
        e = arr[s - 1]
        # shift the elements
        for j in range(s - 1, -1, -1):
            arr[j] = arr[j - 1]

        arr[0] = e
    # display the final array
    print("Output array: ", arr)


def main():
    # take input
    s = int(input("Enter the size of array: "))

    # declare an array of size s
    arr = [0] * s

    # take input
    print("Enter the elements of array: ")
    for i in range(0, s):
        arr[i] = int(input(""))

    # take input of the direction of rotation
    print("Enter the direction of rotation: ")
    print("1. Left rotation")
    print("2. Right rotation")
    d = int(input("Your choice >> "))

    # call function as per choice
    if d == 1:
        leftShift(arr, s)
    elif d == 2:
        rightShift(arr, s)
    else:
        print("Wrong Choice!")


# driver code
main()

Output


Enter the size of array: 5

Enter the elements of array:

0

5

10

15

20

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 1

Enter the number of position to be left shifted: 3

Output array: [15, 20, 0, 5, 10]




2. Time complexity to Rotate an Array


Time Complexity: O(n * k)

Here, $n$ elements in an array are to be rotated by $k$ positions to either left or right.