HackerRank Circular Array Rotation Solution Yashwant Parihar, April 15, 2023April 16, 2023 In this post, we will solve HackerRank Circular Array Rotation Problem. 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 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 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 Explanation 0After 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]]. queries[0] = 0, b[0] = 2. queries[1] = 1, b[1] = 3. queries [2]= 2, b[2] = 1. HackerRank Circular Array Rotation Problem Solution Circular Array Rotation C Solution #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n,k,q,i,x,y; scanf("%d %d %d",&n,&k,&q); int A[n],B[q]; for(i=0;i<n;i++) scanf("%d",&A[i]); for(i=0;i<q;i++) scanf("%d",&B[i]); y = n-k; while(y<0) y = y+n; for(i=0;i<q;i++) { x = (B[i]+y)%n; printf("%d \n",A[x]); } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; } Circular Array Rotation C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n, k, q; cin >> n >> k >> q; vector<int> v; v.reserve(n); int i; while(n--) { cin >> i; v.push_back(i); } vector<int> mv; mv.reserve(q); while(q--) { cin >> i; mv.push_back(i); } for(int m : mv) { int offset = v.size() - (k%v.size()); cout << v[(offset + m)%v.size()] << endl; } return 0; } Circular Array Rotation C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { List<int> inputs = Console.ReadLine().Split(' ').Select(a => Convert.ToInt32(a)).ToList(); List<int> array = Console.ReadLine().Split(' ').Select(a => Convert.ToInt32(a)).ToList(); for (int i = 0; i < inputs[2]; i++) { int pos = Convert.ToInt32(Console.ReadLine()); pos -= inputs[1]; while (pos < 0) pos += inputs[0]; while (pos >= inputs[0]) pos -= inputs[0]; Console.WriteLine(array[pos]); } } } Circular Array Rotation Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'circularArrayRotation' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts following parameters: * 1. INTEGER_ARRAY a * 2. INTEGER k * 3. INTEGER_ARRAY queries */ public static List<Integer> circularArrayRotation(List<Integer> a, int k, List<Integer> queries) { for (int i= 0; i<k; i++) { var temp = a.remove(a.size()-1); a.add(0,temp); } ArrayList<Integer> result = new ArrayList<Integer>(queries.size()); for (var q : queries){ result.add(a.get(q)); } return result; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(firstMultipleInput[0]); int k = Integer.parseInt(firstMultipleInput[1]); int q = Integer.parseInt(firstMultipleInput[2]); List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); List<Integer> queries = IntStream.range(0, q).mapToObj(i -> { try { return bufferedReader.readLine().replaceAll("\\s+$", ""); } catch (IOException ex) { throw new RuntimeException(ex); } }) .map(String::trim) .map(Integer::parseInt) .collect(toList()); List<Integer> result = Result.circularArrayRotation(a, k, queries); bufferedWriter.write( result.stream() .map(Object::toString) .collect(joining("\n")) + "\n" ); bufferedReader.close(); bufferedWriter.close(); } } Circular Array Rotation JavaScript Solution function processData(input) { //Enter your code here input = input.split("\n"); input[0] = input[0].split(" "); var N = parseInt(input[0][0]); var K = parseInt(input[0][1]); var Q = parseInt(input[0][2]); input[1] = input[1].split(" "); var A = []; for (var i=0; i<N; i++) { A.push(parseInt(input[1][i])); } var tempA = []; var relativeShifts = K%N; for (var i=0; i<relativeShifts; i++) { tempA[i] = A[N-relativeShifts+i]; } for (var i=relativeShifts; i<N; i++) { tempA[i] = A[i-relativeShifts]; } A=tempA; var idx; for (var i=0; i<Q; i++) { idx = parseInt(input[i+2]); console.log(A[idx]); } } 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 Python Solution n, k, q = map(int, input().split()) k = k%n a = input().split() for i in range(q): x = int(input()) - k print(a[x%n]) Other Solutions HackerRank Sequence Equation Problem Solution HackerRank Jumping on the Clouds: Revisited c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython