Circular Array Rotation – HackerRank Solution

In this post, we will solve Circular Array Rotation HackerRank Solution. This problem (Circular Array Rotation) is a part of HackerRank Algorithms series.

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 space-separated 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

Solution – Circular Array Rotation – HackerRank Solution

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


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

Python

#!/bin/python3

import sys

def rightRotation(a, d):
    out = list(a)
    a_len = len(a)
    for ind, el in enumerate(a):
        out[(ind + d) % a_len] = el

    return out

def circularArrayRotation(a, m):
    out = []
    for pos in m:
        out.append(a[pos])
        
    return out
    

if __name__ == "__main__":
    n, k, q = input().strip().split(' ')
    n, k, q = [int(n), int(k), int(q)]
    a = list(map(int, input().strip().split(' ')))
    m = []
    m_i = 0
    for m_i in range(q):
        m_t = int(input().strip())
        m.append(m_t)
    a = rightRotation(a, k)
    result = circularArrayRotation(a, m)
    print ("\n".join(map(str, result)))

Java

import java.util.LinkedList;
import java.util.Scanner;

public class Solution
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int q = in.nextInt();
        LinkedList<Integer> list = new LinkedList<Integer>();
        int index;
        
        for(int i = 0; i < n; i++)
        {
            list.add(in.nextInt()); 
        }
        
        while(k > 0)
        {
            int j = list.removeLast();
            list.push(j);
            k--;
        }
        
        while(q > 0)
        {
            index = in.nextInt();
            System.out.println(list.get(index));
            q--;
        }
        in.close();
    }

    
}

Note: This problem (Circular Array Rotation) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *