Reversing
InterviewTime limit2sMemory limit512 MB
Given prefix or suffix reversals applied to an array, track where the element originally at position K ends up after all operations.
- Level
Medium6 of 10
- Topics
- Array, Implementation, Simulation, Math
- Solved
- No attempts yet
Problem
An array holds numbers, and its indices start at 1. You apply operations to the array. Write a program that finds the position of the number that started at index once every operation is done.
One operation is a single nonzero integer . When is positive, the order of the first numbers of is reversed. When is negative, the order of the last numbers is reversed.
For example, take and . Applying operation turns into . Applying operation to that reverses the last four numbers and gives . The number that started at position 1 moves to position 4, and the number that started at position 3 moves to position 1.
Input
The first line contains the size of the array (), the position (), and the number of operations ().
The second line contains the numbers stored in , from the first number to the last. Each of those numbers is a natural number no greater than 100,000. The same number may appear in the array several times.
Each of the next lines contains one integer describing an operation. is a nonzero integer whose absolute value is at most .
Output
On the first line, print the position that the th number has moved to after all operations are done.