Menu Close

Rotate the array in the right direction by K steps

Fazil loves to preform different operations on arrays, and so being the Head of the higher education institution, he assigned a task to his new student Rohan. 

Rohan will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. 

As she is new to the school, please help her to complete the task.

Input:
The first line will consists of one integer T denoting the number of test cases.
For each test case:
The first line consists of two integers N and K, 
N being the number of elements in the array and K denotes the number of steps of rotation.
The next line consists of N space separated integers , denoting the elements of the array A.

Output:
Print the array after rotation in the single line for each test case.
#include <stdio.h>
int main()
{
 int rot[100000];int n,k,j,i,t,tc;
 scanf("%d",&tc);
 while(tc--)
 {
 scanf("%d%d",&n,&k);
 for(j=0;j<n;j++)
 scanf("%d ",&rot[j]);
 for(j=0;j<k;j++)
 {
 t=rot[n-1];
 for(i=n-1;i>0;i--)
 rot[i]=rot[i-1];
 
 rot[i]=t;
 }
 for(i=0;i<n;i++)
 printf("%d ",rot[i]);
 
 printf("\n");
 }
return 0;
}


INPUT_1:
2
5  2
67  45  39  31  56
6  4
14  19  37  62  81  93

OUTPUT:
31  56  67  45  39
37  62  81  93  14  19


INPUT_2:
3
5  1
48  60  15  87  73
4  2
59  30  22  61
3  2
143  23  94

OUTPUT:
73  48  60  15  87
22  61  59  30
23  94  143



Morae! Q