JP and rotations
Practice
3.5 (102 votes)
Ad Hoc
Ready
Hiring
Data structures
Easy
Problem
70% Success 3147 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Kabra is a very good friend of JP. So JP has assigned him a task. Given an array, two operations can be performed on it. They are
1) L X : Rotate the array towards left by X.
2) R X : Rotate the array towards right by X.
Now you will be given 2 arrays containing N unique elements. The first one is the inital array(A) and the second one is target array(T).
Also you will be given a list of M operations having ids from 1 to M to be serially performed on it. If after performing any operation the array becomes same as the target array print the id of the operation. If it is not possible print "-1"(without quotes).

Input format:
First line will contain two space separated integers N and M.
Second line will contain N space separated integers Ai denoting the initial array.
Third line will contain N space separated integers Ti denoting the target array.
Next M lines contain the operations of type 1 or 2.

Output format:
Print a single line containing the required answer.

Constraints:
1 <= N , M <= 105
1 <= Ai , Ti <= 109
1 <= X <= 1000

Note: It is guaranteed that the initial and target arrays are different.
Note: Target array will always be a rotated version of the initial array

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
Tags:
Easy-Medium
Points:20
803 votes
Tags:
Bit manipulationHiringApprovedEasy
Points:50
33 votes
Tags:
Breadth First SearchGraphsMedium
Editorial

Login to unlock the editorial