Cyclic Rotation solution codeforces
There is an array aa of length nn. You may perform the following operation any number of times:
- Choose two indices ll and rr where 1≤l<r≤n1≤l<r≤n and al=aral=ar. Then, set a[l…r]=[al+1,al+2,…,ar,al]a[l…r]=[al+1,al+2,…,ar,al].
You are also given another array bb of length nn which is a permutation of aa. Determine whether it is possible to transform array aa into an array bb using the above operation some number of times.
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of array aa and bb.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — elements of the array aa.
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n) — elements of the array bb.
It is guaranteed that bb is a permutation of aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105
For each test case, print “YES” (without quotes) if it is possible to transform array aa to bb, and “NO” (without quotes) otherwise.
You can output “YES” and “NO” in any case (for example, strings “yEs“, “yes” and “Yes” will be recognized as a positive response).
5 5 1 2 3 3 2 1 3 3 2 2 5 1 2 4 2 1 4 2 2 1 1 5 2 4 5 5 2 2 2 4 5 5 3 1 2 3 1 2 3 3 1 1 2 2 1 1
YES YES NO YES NO
In the first test case, we can choose l=2l=2 and r=5r=5 to form [1,3,3,2,2][1,3,3,2,2].
In the second test case, we can choose l=2l=2 and r=4r=4 to form [1,4,2,2,1][1,4,2,2,1]. Then, we can choose l=1l=1 and r=5r=5 to form [4,2,2,1,1][4,2,2,1,1].
In the third test case, it can be proven that it is not possible to transform array aa to bb using the operation.
ANSWER
“Click Here“

I am the Founder and the creator of the blog neoideasblog.com where we share latest trivia questions and answers on a daily basis.