Wednesday, 3 August 2016

Circular Array Rotation Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int n,k,q,a,m;
 vector<int>ar;
 scanf("%d %d %d",&n,&k,&q);
 for(int i = 0; i < n; i++){
        scanf("%d",&a);
        ar.push_back(a);
 }
 k%=n;;
 for(int i = 0; i < q; i++){
        scanf("%d",&m);
        if(m-k >= 0)printf("%d\n",ar[m-k]);
        else printf("%d\n",ar[m-k+n]);
 }
 return 0;
}

5 comments:

  1. Can you please explain the program?

    ReplyDelete
  2. Replies
    1. try this : http://codesquiz.blogspot.in/2017/01/solved-circular-array-rotation-solution.html

      Delete