Rohan and Rotations

3.4

30 votes
Easy
Problem

Rohan got an array of numbers A from Mohan. Length of array A is n. Mohan asks Rohan to perform 2 different type of operations on array A. The operations are -
(i) Clockwise Rotation - In this operation, the element at index i moves to index (i+1) mod n
(ii) Anticlockwise Rotation - In this operation, the element at index i moves to index (i-1) mod n

Consider the given array as 0-indexed.
Given the initial array A and set of operations, you need to determine the resulting array.

Input Format

First line contains integer n, length of array A
Second line contains n space separated integers denoting the elements of array A
Third line contains integer Q, number of operation to be performed
Fourth line Q space separated integers denoting the set of operations 1 denotes anti-clockwise rotation, while 2 denotes clockwise rotation

Output Format

Output n space-separated integers which is the resulting array

Constraints

  • 1 ≤ n, Q ≤ 100000
  • 1 ≤ A[i] ≤ 109
Sample Input
5
1 2 3 4 5
4
1 1 1 2
Sample Output
3 4 5 1 2
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

After each operation the rotation are as follows -

  • 2, 3, 4, 5, 1
  • 3, 4, 5. 1, 2
  • 4, 5. 1, 2, 3
  • 3, 4, 5. 1, 2

So the resulting array is 3, 4, 5. 1, 2

Contributers:
Editor Image

?