#K70657. Rotate Array and Answer Queries

    ID: 33357 Type: Default 1000ms 256MiB

Rotate Array and Answer Queries

Rotate Array and Answer Queries

You are given an array A of N integers. The task is to perform a left rotation on the array by D positions, and then answer Q queries. In each query, you are given an index, and you need to output the element at that index in the rotated array.

Rotation Details:

  • The left rotation is defined as moving the first D elements of the array to the end in the same order.
  • If D is greater than N, you should use the formula $$D = D \mod N$$ to normalize the number of rotations.

Example: For N = 5, D = 2 and A = [1, 2, 3, 4, 5], after rotation the array becomes [3, 4, 5, 1, 2]. A query asking for index 0 returns 3, index 2 returns 5, etc.

The solution needs to process input from standard input (stdin) and produce output on standard output (stdout). Ensure that your code is efficient enough to handle large inputs.

inputFormat

The input is given via standard input (stdin) and has the following format:

  1. The first line contains three integers N, D, and Q, where N is the length of the array, D is the number of left rotations, and Q is the number of queries.
  2. The second line contains N space-separated integers denoting the array A.
  3. The third line contains Q space-separated integers, each representing a query index.

outputFormat

Print the answers to the queries on a single line, with each answer separated by a space. The answer for each query is the element in the rotated array at the queried index.

## sample
5 2 3
1 2 3 4 5
0 2 4
3 5 2