Header Ad

HackerRank Circular Array Rotation problem solution

In this HackerRank Circular Array Rotation problem For each array, perform a number of right circular rotations and return the values of the elements at the given indices.

HackerRank Circular Array Rotation problem solution


Problem solution in Python programming.

N, K, Q= map(int, input().split())
A= tuple(map(int, input().split()))
for _ in range(Q):
    print(A[(int(input())+N-K)%N])


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws IOException {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(in.readLine());
        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());
        int Q = Integer.parseInt(st.nextToken());
        StringTokenizer array = new StringTokenizer(in.readLine());
        ArrayList<Integer> a = new ArrayList<Integer>(N);
        for (int i = 0; i < N; i++) {
            a.add(Integer.parseInt(array.nextToken()));
        }
        List<Integer> subListOne = a.subList(0, (N - (K % N)));
        a.addAll(subListOne);
        
        // Queries
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < Q; i++) {
            int x = Integer.parseInt(in.readLine());
            output.append(a.get(x + (N - (K % N)))).append("\n");
        }
        System.out.print(output.toString());
    }
}


Problem solution in C++ programming.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX 100010

using namespace std;

int a[MAX];

int main() {
    int n, m, k, x, i;

    while (~scanf("%d %d %d", &n, &k, &m)) {
        k %= n;
        for (i = 0; i < n; ++i) scanf("%d", &a[i]);
        reverse(a, a + n - k);
        reverse(a + n - k, a + n);
        reverse(a, a + n);
        while (m--) {
            scanf("%d", &x);
            printf("%d\n", a[x]);
        }
    }

    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int32_t N, numOps, queries, temp;
    int32_t i, j;

    scanf("%d", &N) ;
    scanf("%d", &numOps);
    scanf("%d", &queries);

    int32_t array[N], tempArray[N], indexOfAnswers[queries];

    for(i = 0; i < N; ++i)
        scanf("%d", &array[i]);
    for(i = 0; i < queries; ++i)
        scanf("%d", &indexOfAnswers[i]);

    for(i = 0; i < queries; i++)
    {
        printf("%d\n",
          array[
                indexOfAnswers[i] >= numOps ?
               ((indexOfAnswers[i] - numOps) - (((indexOfAnswers[i] - numOps)/N) * N)) % N:
        (((indexOfAnswers[i] - numOps) - (((indexOfAnswers[i] - numOps)/N) * N)) + N) % N
              ]);
    }

    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var data = input.split('\n');
    var k = parseInt(data[0].split(' ')[1], 10);
    var ns = data[1].split(' ').map(function(s) { return parseInt(s, 10); });
    var qs = data.slice(2).map(function(s) { return parseInt(s, 10); });
    var rotated = [];
    var i, newIndex;

    for (i = 0; i < ns.length; i++) {
        newIndex = (k + i + 1) % ns.length;
        if (newIndex === 0) newIndex = ns.length - 1;
        else newIndex--;
        rotated[newIndex] = ns[i];
    }
    for (i = 0; i < qs.length; i++) {
        console.log(rotated[qs[i]]);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments