Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python program for finding the circular rotations of array by k positions

Circular-rotation-of-an-array

Circular rotation of an array by K position

Here in this programme, we will learn about circular array rotation, which means rotating the elements in the array .The operation of the rotation moves the last element of the array to the first position and moves all the remaining elements to the right. Take into consideration the following [0,1,2] indexes to be checked.

Algorithm

Step 1- Initialize a class

Step 2- Enter number of elements of array

Step 3- Enter number of rotations of array.

Step 4- Enter number of indexes to be displayed.

Step 5- Input array elements

Step 6- run a for loop, i=0; i< elements; , i++.

Step 7- then module your rotations with elements

Step 8- Enter the index of array to be displayed

Step 9- print number of indexes and rotations.

 

Python Program

 

a=list(map(int,input("ENTER ARRAY ELEMENTS ").split()))
k=int(input("ENTER NO.OF ROTATIONS "))
print("ARRAY BEFORE ROTATION ",*a)
k=k%len(a)
for i in range(k):
    x=a.pop(-1)
    a.insert(0,x)
print("ARRAY AFTER  ROTATION  ",*a)