Larry's Array ⬀
Larry has been given a permutation of a sequence of natural numbers incrementing from 1 as an array. He must determine whether the array can be sorted using the following operation any number of times:
- Choose any
3consecutive indices and rotate their elements in such a way thatABC → BCA → CAB → ABC
For example, if A = {1, 6, 5, 2, 4, 3}:
A rotate
[1,6,5,2,4,3] [6,5,2]
[1,5,2,6,4,3] [5,2,6]
[1,2,6,5,4,3] [5,4,3]
[1,2,6,3,5,4] [6,3,5]
[1,2,3,5,6,4] [5,6,4]
[1,2,3,4,5,6]
YES
On a new line for each test case, print YES if A can be fully sorted. Otherwise, print NO.
Complete the larrysArray function in the editor below. It must return a string, either YES or NO.
larrysArray has the following parameter(s):
A: an array of integers
The first line contains an integer t, the number of test cases.
The next t pairs of lines are as follows:
- The first line contains an integer
n, the length ofA. - The next line contains
nspace-separated integersA[i].
1 ≤ t ≤ 103 ≤ n ≤ 10001 ≤ A[i] ≤ nAsorted =integers that increment by1from1ton
For each test case, print YES if A can be fully sorted. Otherwise, print NO.
3
3
3 1 2
4
1 3 4 2
5
1 2 3 5 4
YES
YES
NO
In the explanation below, the subscript of A denotes the number of operations performed.
A₀ = {3, 1, 2} → rotate(3, 1, 2) → A₁ = {1, 2, 3}Ais now sorted, so we printYESon a new line.
A₀ = {1, 3, 4, 2} → rotate(3, 4, 2) → A₁ = {1, 4, 2, 3}A₁ = {1, 4, 2, 3} → rotate(4, 2, 3) → A₂ = {1, 2, 3, 4}Ais now sorted, so we printYESon a new line.
No sequence of rotations will result in a sorted A. Thus, we print on a new line.