Circular Array Rotation

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 value of the element at a given index.

For example, array a = [3,4,5], number of rotations, k = 2 and indices to check, m = [1,2].

First we perform the two rotations:

[3,4,5] -> [5,3,4] -> [4,5,3]

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

a[1] = 5

a[2] = 3

Function Description

Complete the circularArrayRotation function in the editor below. It should return an array of integers representing the values at the specified indices.

circularArrayRotation has the following parameter(s):

  • a: an array of integers to rotate
  • k: an integer, the rotation count
  • queries: an array of integers, the indices to report

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 denoting m, the index of the element to return from a.

Constraints

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

Output Format

For each query, print the value of the element at index m of the rotated array on a new line.

Sample Input 0

1
2
3
4
5
3 2 3
1 2 3
0
1
2

Sample Output 0

1
2
3
2
3
1

Explanation 0

After the first rotation, the array becomes [3,1,2].

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

Let’s refer to the array’s final state as array b = [2,3,1]. For each query, we just have to print the value of bm on a new line:

  1. m = 0, b0 = 2.
  2. m = 1, b1 = 3.
  3. m = 2, b2 = 1.

Solution

1
2
3
4
5
6
7
8
9
10
// Complete the circularArrayRotation function below.
function circularArrayRotation(a, k, queries) {
//Enter your code here
return queries.map(value => a.reduce((target, item, index) => {
let focus = (index + k) % a.length;
target[focus] = item;

return target;
}, [])[value]);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×