Amazon Question - 3 Rotate Array
Given an array of size N, rotate it by D elements.
Input:
The first line of the input contains T denoting the number of testcases. First line of test case is the number of elements N, next line contains D. Subsequent line will be the array elements.
The first line of the input contains T denoting the number of testcases. First line of test case is the number of elements N, next line contains D. Subsequent line will be the array elements.
Output:
For each testcase, in a new line, output the rotated array.
For each testcase, in a new line, output the rotated array.
Constraints:
1 <= T <= 200
1 <= N <= 107
1 <= D <= N
1 <= arr[i] <= 103
1 <= T <= 200
1 <= N <= 107
1 <= D <= N
1 <= arr[i] <= 103
Example:
Input:
2
5 2
1 2 3 4 5
10 3
2 4 6 8 10 12 14 16 18 20
Input:
2
5 2
1 2 3 4 5
10 3
2 4 6 8 10 12 14 16 18 20
Output:
3 4 5 1 2
8 10 12 14 16 18 20 2 4 6
3 4 5 1 2
8 10 12 14 16 18 20 2 4 6
Code C - Language :
#include<stdio.h>
int main() {
//code
int tc,n,i,j,d;
scanf("%d",&tc);
while(tc--){
scanf("%d",&n);
scanf("%d",&d);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=d;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<d;i++)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}
Comments
Post a Comment