Circular rotation of an array by K positions | Faceprep

15 min read

Circular array rotation means rotating the elements in the array where one rotation operation moves the last element of the array to the first position and shifts all remaining elements to the right.

For example, consider the following array = [4, 5, 6], number of rotations = 2,  indexes to be checked = [0,1,2]

  • Initial array    [4, 5, 6]
  • After one rotation    [6, 4, 5]
  • After two rotations    [5, 6, 4]

OUTPUT

Element at index 0 : 5

Element at index 1 : 6

Element at index 2 : 4

 

C++ program to circular rotate the array by K positions

#include <iostream>
using namespace std;

int main()
{
int no_of_elements, no_of_rotations, no_to_display;

cout<<"Enter the number of elements of the array : ";
cin >> no_of_elements;

cout<<"Enter the number of rotations of the array : ";
cin>>no_of_rotations;

cout<<"Enter the number of indexes to be displayed : " ;
cin>>no_to_display;

int arr[no_of_elements];
cout<<"\n Input the array elements\n";
for(int i = 0; i < no_of_elements; i++)
{
cin>>arr[i];
}

cout<<"\nThe Array Elements are\n";

for(int i = 0; i < no_of_elements; i++)
{
cout<<arr[i];
cout<<"\t";
}

no_of_rotations %= no_of_elements; // Remove the number of full array rotations from k

for(int i = 0; i < no_to_display; i++)
{
int index;
cout<<"\nEnter the index of the array to be displayed : ";
cin>>index;
cout<<"\nThe element in the array is ";
cout<<arr[(no_of_elements + index - no_of_rotations) % no_of_elements]<<"\n"; // Calculate the new index 
}

return 0;
}

 

Java program to circular rotate the array by K positions

import java.util.*;
public class Main
{
public static void main(String[] args) 
{
int no_of_elements, no_of_rotations, no_to_display;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of elements of the array : ");
no_of_elements = sc.nextInt();

System.out.println("Enter the number of rotations of the array : ");
no_of_rotations = sc.nextInt();

System.out.println("Enter the number of indexes to be displayed : ");
no_to_display = sc.nextInt();

int arr[] = new int[no_of_elements];
System.out.println("\n Input the array elements\n");
for(int i = 0; i < no_of_elements; i++)
{
arr[i] = sc.nextInt();
}

System.out.println("\nThe Array Elements are\n");
for(int i = 0; i < no_of_elements; i++)
{
System.out.print(arr[i]);
System.out.print("\t");
}

no_of_rotations %= no_of_elements; // Remove the number of full array rotations from k

for(int i = 0; i < no_to_display; i++)
{
int index;
System.out.println("\nEnter the index of the array to be displayed : ");
index = sc.nextInt();

System.out.println("\nThe element in the array is ");
System.out.println(arr[(no_of_elements + index - no_of_rotations) % no_of_elements]); // Calculate the new index 
}}}


 

Python program to circular rotate the array by K positions

import array as arr
arr = arr.array('i', [1,2,3])
print("Enter the number of elements of the array : ")
no_of_elements = int(input())

print("Enter the number of rotations of the array : ")
no_of_rotations = int(input())

print("Enter the number of indexes to be displayed : ")
no_to_display = int(input())
print("The array elements are")
for i in range(len(arr)):
print(arr[i], end = " ")

no_of_rotations %= no_of_elements; # Remove the number of full array rotations from k

for i in range(0,no_to_display):
print("\nEnter the index of the array to be displayed : ")
index = int(input())
print("\nThe element in the array is ")
print(arr[(no_of_elements + index - no_of_rotations) % no_of_elements]); # Calculate the new index


OUTPUT

circular array rotation
 
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error