(RankList for this Question)
A teacher had a task of grading answer sheets of students, each answer sheet had roll numbers written on it. The teacher is lazy and wants to arrange the answer sheets in increasing order (from top to bottom) of the roll numbers using at most one REARRANGEMENT move.
A REARRANGEMENT move means taking any fixed number of answer sheets from the top of the answer sheet pile and placing them at the bottom of the pile in the same relative order.
You are a Student and you have to convince your teacher that sometimes it is not possible to arrange the answer sheets in increasing order using at most one REARRANGEMENT move. That is why you need to write a program that determines if it is possible to arrange the pile of answer sheets or not.
2 ≤ N ≤ 10^5
1≤ R[i] ≤ 10^9 for each valid i
The first line of each test case contains a single integer N.
The second line contains N space-separated integers R1,R2,…,RN representing the roll number on each answer sheet in the pile from top to bottom.
For each test case, print a single line containing the string "YES" if it is possible to arrange the answer sheets or "NO" if it is not possible.
1 5 2 4 3
No possible combination of cards can be moved from top to bottom to make the resultant order in increasing manner.
3 4 5 1 2
3,4,5 can be shifted from top to bottom resulting in 1,2,3,4,5 which is in increasing order hence it is possible
Log In to solve the Question