# Answers for "Rotation of array in C++"

2

rotate an array right in c

``````// C program to rotate an array cyclically

#include <stdio.h>

void rightRotateByOne(int arr[], int n) //function for cyclically rotating an array once
{
int x = arr[n-1], i;
for (i = n-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = x;
}

int main()
{int t;
scanf("%d",&t);//number of test cases
int p;
for(p=0;p<t;p++){
int n,i,k;
scanf("%d %d",&n,&k); // n--> size of array ; k--> number of rotations
int arr[n];
k=k%n;
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int j;
for(j=0;j<k;j++) //cyclically rotating an array k times
{rightRotateByOne(arr, n);}

for (i = 0; i < n; i++){
printf("%d ", arr[i]);}
printf("\n");}

return 0;
}``````
Posted by: Guest on October-20-2020
0

rotate array c#

``````List<int> iList = new List<int>();

private void shift(int n)
{
for (int i = 0; i < n; i++)
{
iList.RemoveAt(0);
}

}``````
Posted by: Guest on October-24-2020
0

Rotation of array in C++

``````Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store the first d elements in a temp array
temp[] = [1, 2]
2) Shift rest of the arr[]
arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
arr[] = [3, 4, 5, 6, 7, 1, 2]``````
Posted by: Guest on May-01-2021