Monk and Rotation [Hacker earth]

1–2 minutes

read

Monk and Rotation [Hacker earth]

Advertisements

PROBLEM:

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 AA of size NN and an integer KK , 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 TT denoting the number of test cases.
For each test case:
1) The first line consists of two integers NN and KKNN being the number of elements in the array and KK denotes the number of steps of rotation.
2) The next line consists of NN space separated integers , denoting the elements of the array AA.

Output:
Print the required array.

Screen Shot 2017-12-16 at 11.43.56 AM

SOLUTION:

#include<iostream>
using namespace std;
int main()
{    int t;    
     cin>>t;
     while(t--)    
     {        
          int n,k,p;        
          cin>>n;        
          int a[n];
          cin>>k;
          for(int i=0;i<n;i++)
          {
              cin>>a[i];        
          }        
          k%=n;
          for(int i=0;i<n;i++)
          {
               p = a[(i+(n-k))%n];
               cout<<p<<" ";
          } cout<<"\n";
      }
return 0;
}

OUTPUT:

Screen Shot 2017-12-16 at 11.43.24 AM

IMAGES CREDIT: Hacker earth

Advertisements

4 responses to “Monk and Rotation [Hacker earth]”

  1. Nice logic without any extra space complexity

    Liked by 1 person

  2. Pranoti Pawan Pande Avatar
    Pranoti Pawan Pande

    can uh please explain i am not understading this logic

    Like

  3. Shivam Panchbhai Avatar
    Shivam Panchbhai

    thank you very much

    Like

Leave a comment