# HackerRank Circular Array Rotation Solution

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

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank Circular Array Rotation

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 sh = getShift(params._1, params._2)
val res = input.takeRight(sh) ++ input.dropRight(sh)

for (i <- 0 until params._3) {
}
}
}```

### 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
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
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 Chase2learn. This tutorial is only for Educational and Learning purposes.

## FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.