Monk and Rotation Hackerearth problem solution

Monk and Rotation Hackerearth

Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki 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:
1) 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.
2) The next line consists of $$N$$ space separated integers , denoting the elements of the array $$A$$.
Output:
Print the required array.

Constraints:
$$1 \le T \le 20$$
$$1 \le N \le 10^5$$
$$0 \le K \le 10^6$$
$$0 \le A[i] \le 10^6$$
SAMPLE INPUT
1
5 2
1 2 3 4 5
SAMPLE OUTPUT
4 5 1 2 3
Explanation
Here $$T$$ is 1, which means one test case.
$$N = 5 $$ denoting the number of elements in the array and $$ K = 2 $$, denoting the number of steps of rotations.
The initial array is: $$ 1, 2, 3, 4, 5 $$
In first rotation, $$5$$ will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be $$ 5, 1, 2, 3, 4 $$
In second rotation, $$4$$ will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be $$ 4, 5, 1, 2, 3 $$

Solution

Monk and Rotation Hackerearth

#include <stdio.h>
int main()
{

int t;
scanf("%d", &t);
while(t--)
{
long int n,k,pos,size, i;
scanf("%lld %lld", &n,&k);
long int a[n],x;
k=k%n;
for(i=0;i<n;i++)
{

scanf("%lld", &x);
pos=((i+k)>=n)?((i+k)-n):(i+k);
a[pos]=x;
}
for(i=0;i<n;i++)
{
printf("%lld ",a[i]);
}
printf("\n");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k;
		int arr[100005];
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++)
			scanf("%d",&arr[i]);
		k%=n;
		for(int i=0;i<n;i++)
			printf("%d ",arr[(i+(n-k))%n]);
		printf("\n");
	}
	return 0;
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.