# Circular Array Rotation HackerRank Solution in C, C++, Java, Python

John Watson knows of an operation called a right circular rotation on an array of integers. One rotation operation moves the last array element to the first position and shifts all remaining elements right one. To test Sherlock’s abilities, Watson provides Sherlock with an array of integers. Sherlock is to perform the rotation operation a number of times then determine the value of the element at a given position.

For each array, perform a number of right circular rotations and return the values of the elements at the given indices.

Example

a = [3,4,5]

k = 2

queries = [1,2]

Here k is the number of rotations on a, and queries holds the list of indices to report. First we perform the two rotations: [3,4,5] -> [5,3,4] -> [4,5,3]

Now return the values from the zero-based indices 1 and 2 as indicated in the queries array.

a[1] = 5

a[5] = 3

Function Description

Complete the circularArrayRotation function in the editor below.

circularArrayRotation has the following parameter(s):

• int a[n]: the array to rotate
• int k: the rotation count
• int queries[1]: the indices to report

Returns

• int[q]: the values in the rotated  as requested in

Input Format

The first line contains 3 space-separated integers, n, k, and q, the number of elements in the integer array, the rotation count and the number of queries.

The second line contains n space-separated integers, where each integer i describes array element  a[i] (where 0<=i<n ).

Each of the q subsequent lines contains a single integer,queries[i] , an index of an element in a to return.

Sample Input 0

```3 2 3

1 2 3

0

1

2```

Sample Output 0

```2

3

1
```

## Circular Array Rotation HackerRank Solution in C

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

long n,k,q,i;
long a[100000];
scanf("%ld%ld%ld",&n,&k,&q);
long r=k%n;
for(i=r;i<n;i++)
scanf("%ld",&a[i]);
for(i=0;i<r;i++)
scanf("%ld",&a[i]);
for(i=0;i<q;i++)
{
scanf("%ld",&k);
printf("%ld\n",a[k]);
}

return 0;
}```

## Circular Array Rotation HackerRank Solution in C++

```#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define FOR(i,s,e) for (int i = int(s); i < int(e); i++)
#define FORIT(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define sz(v) (int)v.size()
#define all(c) (c).begin(), (c).end()

typedef long long int ll;

// %I64d for ll in CF

int main() {
int n, k, q;
while (scanf("%d %d %d", &n, &k, &q) != EOF) {
int as[n];
for (int i = 0; i < n; i++) {
scanf("%d", &as[i]);
}

for (int i = 0; i < q; i++) {
int x;
scanf("%d", &x);
x -= k;
while (x < 0) x += n;
printf("%d\n", as[x]);
}
}
return 0;
}```

## Circular Array Rotation HackerRank Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt(), q = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = in.nextInt();
for(int i = 0; i < q; i++) System.out.println(a[(n+((in.nextInt()-k)%n)) % n]);
}
}```

## Circular Array Rotation HackerRank Solution in Python

```(N,K,Q) = tuple(map(int,raw_input().split(" ")))
a = map(int, raw_input().split(" "))
for q in xrange(Q):
query = input()
print a[(query - K) % N]```

## Circular Array Rotation HackerRank Solution in C#

```using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {

int N=Convert.ToInt32(input[0]);
int K=Convert.ToInt32(input[1]);
int Q=Convert.ToInt32(input[2]);

int[] polje=new int[N];

for (int i=0;i<N;i++){
polje[i]=Convert.ToInt32(input[i]);
}

for (int i=0;i<Q;i++){

int poz=x-K;

while (poz<0){
poz+=N;
}

Console.WriteLine(polje[poz]);
}
}
}```

Attempt Circular Array Rotation HackerRank Challenge

Next HackerRank Challenge Solution