2017-02-16 23:51:56 -06:00
|
|
|
// Author: Rodney Shaghoulian
|
2018-07-27 16:45:56 -07:00
|
|
|
// Github: github.com/RodneyShag
|
|
|
|
|
// HackerRank: hackerrank.com/RodneyShag
|
2017-02-16 23:51:56 -06:00
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2017-04-05 18:42:35 -05:00
|
|
|
// Time Complexity: O(n)
|
|
|
|
|
// Space Complexity: O(1) by doing an "in place" rotation
|
2017-02-16 23:51:56 -06:00
|
|
|
public class Solution {
|
|
|
|
|
public static void main(String[] args) {
|
2017-04-05 18:42:35 -05:00
|
|
|
/* Save input */
|
2017-02-16 23:51:56 -06:00
|
|
|
Scanner scan = new Scanner(System.in);
|
2017-04-05 18:42:35 -05:00
|
|
|
int size = scan.nextInt();
|
|
|
|
|
int k = scan.nextInt();
|
|
|
|
|
int q = scan.nextInt();
|
|
|
|
|
int [] array = new int[size];
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
2017-02-16 23:51:56 -06:00
|
|
|
array[i] = scan.nextInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Rotate array (in place) using 3 reverse operations */
|
2017-04-05 18:42:35 -05:00
|
|
|
k %= size; // to account for k > size
|
2017-02-16 23:51:56 -06:00
|
|
|
reverse(array, 0, array.length - 1);
|
|
|
|
|
reverse(array, 0, k - 1);
|
|
|
|
|
reverse(array, k, array.length - 1);
|
|
|
|
|
|
|
|
|
|
/* Print output */
|
2017-04-05 18:42:35 -05:00
|
|
|
while (q-- > 0) {
|
2017-02-16 23:51:56 -06:00
|
|
|
int m = scan.nextInt();
|
|
|
|
|
System.out.println(array[m]);
|
|
|
|
|
}
|
|
|
|
|
scan.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Reverses array from "start" to "end" inclusive */
|
|
|
|
|
private static void reverse(int [] array, int start, int end) {
|
|
|
|
|
if (array == null || start < 0 || start >= array.length ||
|
|
|
|
|
end < 0 || end >= array.length || start >= end) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int mid = (start + end) / 2;
|
|
|
|
|
for (int i = start; i <= mid; i++) {
|
|
|
|
|
int offset = i - start;
|
|
|
|
|
swap(array, start + offset, end - offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-05 18:42:35 -05:00
|
|
|
|
|
|
|
|
private static void swap(int [] array, int i, int j) {
|
2017-02-16 23:51:56 -06:00
|
|
|
int temp = array[i];
|
|
|
|
|
array[i] = array[j];
|
|
|
|
|
array[j] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|