Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
We rotate the array in circles. That why it is enough to calculate a position after all rotations using modulo. We split the array into two halves at position array_length - calculated_position
. We change halves and concat them – the second will be first and the first will be the second.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.*; public class CircularArrayRotation { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); int k = stdin.nextInt(); int q = stdin.nextInt(); List<Integer> numbers = new ArrayList<>(n); for(int i = 0; i < n; i++) { numbers.add(stdin.nextInt()); } int[] queries = new int[q]; for(int i = 0; i < q; i++) { queries[i] = stdin.nextInt(); } int rotationPos = k % n; List<Integer> part1 = numbers.subList(0, numbers.size() - rotationPos); List<Integer> part2 = numbers.subList(numbers.size() - rotationPos, numbers.size()); List<Integer> finalList = new ArrayList<>(part2); finalList.addAll(part1); for(int i = 0; i < q; i++) { System.out.println(finalList.get(queries[i])); } stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
'use strict'; const processData = input => { const lines = input.split('\n'); const [n, k, q] = lines[0].split(' ').map(i => parseInt(i)); const elements = lines[1].split(' ').map(i => parseInt(i)); const queries = lines.slice(2).map(i => parseInt(i)); const rotationPos = k % n; const part1 = elements.slice(0, n - rotationPos); const part2 = elements.slice(n - rotationPos); const finalArray = part2.concat(part1); for(let i = 0; i < q; i++) { console.log(finalArray[queries[i]]); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import scala.io.Source object CircularArrayRotation extends App { val lines = Source.stdin.getLines().toList val line1 = lines.head.split(" ").map(_.toInt) val (n, k, q): (Int, Int, Int) = line1 match { case (List(a, b, c)) => (a, b, c) case Nil => throw new IllegalArgumentException } val elements = lines.tail.head.split(" ").map(_.toInt) val ms = lines.tail.tail.map(_.toInt) val rotationPos = k % n val parts = elements.splitAt(elements.length - rotationPos) val newElements = parts._2.toList ::: parts._1.toList ms.foreach(m => { println(newElements(m)) }) } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(n, k, q) = gets.split(" ").map(&:to_i) elements = gets.split(" ").map(&:to_i) queries = [] q.times do queries << gets.to_i end rotationPos = k % n part1 = elements.slice(0, n - rotationPos) part2 = elements.slice(n - rotationPos, n) final_array = part2.concat(part1) q.times do |i| puts final_array[queries[i]] end |