# HackerRank Circular Array Rotation Solution

#### ByBrokenprogrammers

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Circular Array Rotation Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Circular Array Rotation Solution

### Task

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[2] = 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 a as requested in m.

Input Format

The first line contains 3 spaceseparated integers, nk, 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.

Constraints

• 1 <= n <= 105
• 1 <= a[i] <= 105
• 1 <= k <= 105
• 1 <= q <= 500
• 0 <= queries[i] < n

Sample Input 0

```3 2 3
1 2 3
0
1
2
```

Sample Output 0

```2
3
1
```

Explanation 0

After the first rotation, the array is [3, 1, 2].
After the second (and final) rotation, the array is [2, 3, 1].

We will call this final state array b = [2, 3, 1]. For each query, we just have to get the value of b[queries[i]].

1. queries[0] = 0, b[0] = 2
2. queries[1] = 1, b[1] = 3
3. queries[2] = 2, b[2] = 1

## HackerRank Circular Array Rotation Solution

### Circular Array Rotation 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]);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}```

### Circular Array Rotation Solution in Cpp

```#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 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 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 Solution using JavaScript

```  var processData, rotate;
processData = function(input) {
var K, N, Q, arr, data, i, _i, _ref;
data = input.split('\n');
_ref = data[0].split(' '), N = _ref[0], K = _ref[1], Q = _ref[2];
arr = data[1].split(' ');
arr = rotate(arr, K);
for (i = _i = 0; 0 <= Q ? _i < Q : _i > Q; i = 0 <= Q ? ++_i : --_i) {
console.log(arr[data[i + 2]]);
}
};
rotate = function(arr, K) {
var last;
K = K % arr.length;
if (K) {
last = arr.splice(-K);
last = last.concat(arr);
return last;
} else {
return arr;
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});```

### Circular Array Rotation Solution in Scala

```object Solution {
def main(args: Array[String]) {

def getShift(p1: Int, p2: Int) = {
if (p2 >= p1) {
p2 % p1
} else {
p2
}
}
val params = Console.readLine.split(" ").map(_.toInt) match {
case Array(n, k, q) => (n, k, q)
}
val input = Console.readLine.split(" ").map(_.toInt)
val sh = getShift(params._1, params._2)
val res = input.takeRight(sh) ++ input.dropRight(sh)

for (i <- 0 until params._3) {
println(res(Console.readLine.toInt))
}
}
}```

### Circular Array Rotation Solution in Pascal

```(* Enter your code here. Read input from STDIN. Print output to STDOUT *)const
tfi='';
tfo='';
var
fi,fo:text;
a:array[0..200000] of longint;
n,k,q:longint;
procedure nhap;
var i:longint;
begin
read(fi,n,k,q);
for i:=0 to n-1 do read(fi,a[i]);
for i:=n to 2*n-1 do a[i]:=a[i-n];
end;
procedure process;
var i,j,tg:longint;
begin
tg:=n-k mod n;
if tg=0 then tg:=n;
for i:=1 to q do
begin
read(fi,j);
writeln(fo,a[tg+j]);
end;
end;
BEGIN
assign(fi,tfi);reset(fi);
assign(fo,tfo);rewrite(fo);
nhap;
process;
close(fi);close(fo);
END.
```

Disclaimer: This problem (Circular Array Rotation) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.