# Array Rotation in C++

## Rotation of array in C++

Array Rotation in C can be of 2 types

1. Right Rotation of elements
i.e moving the elements of array to the right side or clockwise direction.

2. Left Rotation of elements
i.e moving the elements of array to the left side or anticlockwise direction.

In this page we will look into a coding question that will clear the concept how we can do the rotation of array in C++ programming Language . ### Problem Statement

Write a program in C++ program where you need to do Array Rotation as per the input of question.

Input Format
–
First line predicts the total number of elements present int the array.
-Second line contains the elements of array
-Third line contains the number of time rotation need to be done
-Fourth line contains the character ‘L’ or ‘R’ that defines what type of rotation needs to be done.

Output Format

-Output contains only 1 line that is the resultant output Sample Test Case : 1
Input
5
1 2 3 4 5
4
L

Output
5 1 2 3 4 Sample Test Case : 2
Input
7
10 11 12 13 14 15 16
4
R

Output
13 14 15 16 10 11 12 ```#include

using namespace std;

int rotLeft(int arr[], int n, int d)
{
int i, j;
int first;
for(i=0; i<d; i++)
{
first = arr;
for(j=0; j<n-1; j++)
{
arr[j] = arr[j+1];
}
arr[j] = first;
}
return *arr;
}
int rotRight(int arr[], int n, int d)
{
int i, j;
int first;
for(i=0; i<d; i++) { first = arr[n-1]; for(j=n-1; j>=0; j--)
{
arr[j] = arr[j-1];
}
arr = first;
}
return *arr;
}
int main()
{
int n, d, i;
char choice;
cout<<"Enter size of the Array = "; cin>>n;
int list[n];
cout<<"Enter element of the Array = ";
for(i=0; i<n; i++) { cin>>list[i];
}
cout<<"Enter number of time rotation need to be done = "; cin>>d;
cout<<"What type of rotation need to be done (L/R)? = "; cin>>choice;
if(choice=='L')
{
rotLeft(list, n, d);
for(i=0; i<n; i++)
{
cout<<list[i]<<" ";
}
}
else if(choice=='R')
{
rotRight(list, n, d);
for(i=0; i<n; i++)
{
cout<<list[i]<<" ";
}
}
else
{
cout<<"Not a valid choice";
}
return 0;
}

```

Output:-

```Enter size of the Array = 5
Enter element of the Array = 1
2
3
4
5
Enter number of time rotation need to be done = 3
What type of rotation need to be done (L/R)? = L
4 5 1 2 3Enter size of the Array = 6Enter element of the Array = 123478Enter number of time rotation need to be done = 2What type of rotation need to be done (L/R)? = L3 4 7 8 1 2 Enter size of the Array = 10Enter element of the Array = 1214223778110111318Enter number of time rotation need to be done = 3What type of rotation need to be done (L/R)? = L3 77 81 10 11 13 18 12 14 22 ```