The match is set and your team is ready to win!

The team has $N$ players, each with the ability $A_{i}$.

But you are not happy with the given playing order. You are wondering how to create a new order so that the player with lesser ability go earlier, i.e., in non-decreasing order.

In one move, you can break a suffix of any length of the array and attach it to the front. You can do the move as many times as you like.

For example, the Array $A = [1, 2, 3, 4, 5, 6]$ can become $A = [5, 6, 1, 2, 3, 4]$ in one move by breaking and reattaching the suffix of length $2$.

For each testcase, if the array can be sorted non-decreasingly, print YES and the minimum number of moves to do so. Otherwise, print NO.

------  Input  ------ 

The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
Each testcase contains two lines. The first line has a single integer $N$, denoting the number of players in the team.
The second list has $N$ space-separated integers of the array $A$, where $A_{i}$ represents the ability of the $i^{th}$ player

------  Output  ------ 

Print a single line containing the string "YES" if the string can be sorted or "NO" if it does not (without quotes). If answer is yes, print a second line containing a single integer, the minimum number of moves.

You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).

------ Constraints ------ 

$ $1 ≤ T, N ≤ 3 \cdot 10^{5}$
$$ $0 ≤ A_{i} ≤ 10^{9}$
$$ The sum of all $N$ over all testcases $T$, does not exceed $3 \cdot 10^{5}$.

----- Sample Input 1 ------ 
2
2
1 2
3
2 1 3
----- Sample Output 1 ------ 
YES
0
NO
----- explanation 1 ------ 
Example Case 1: The array is already sorted non-decreasingly. Hence, 0 moves required.Example Case 2: We cannot satisfy the condition for the given array. Hence, answer is NO.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			s += 1
		if s > 1:
			print('NO')
			break
	else:
		if s == 0:
			print('YES')
			print(0)
		elif a[0] < a[-1]:
			print('NO')
		else:
			print('YES')
			print(s)


from collections import defaultdict as dd
t1 = int(input())
for _ in range(t1):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			c += 1
	if c == 0:
		print('YES')
		print(0)
	elif c == 1 and a[n - 1] <= a[0]:
		print('YES')
		print(1)
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			c += 1
	if c == 0:
		print('YES')
		print(0)
	elif c == 1 and l[-1] <= l[0]:
		print('YES')
		print(1)
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(b, s) = (10 ** 11, a[0])
	f = 1
	c = 0
	for i in range(n):
		if a[i] > b:
			print('NO')
			f = 0
			break
		if i + 1 < n and a[i] > a[i + 1]:
			b = s
			s = a[i + 1]
			c += 1
	if f:
		print('YES')
		print(c)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(10 ** 9)
	f = 0
	c = 0
	big = float('inf')
	small = a[0]
	for i in range(n):
		first = a[i]
		se = a[i + 1]
		if first > big:
			print('NO')
			f = 1
			break
		if first > se:
			big = small
			c += 1
			small = se
	if f == 0:
		print('YES')
		print(c)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = sorted(l)
	if l1 == l:
		print('YES')
		print(0)
	elif l[0] == min(l):
		l2 = l[::-1]
		i = l2.index(max(l))
		i = n - 1 - i
		l3 = l[i + 1:]
		l4 = l[:i + 1]
		if l3 + l4 == l1:
			print('YES')
			print(1)
		else:
			print('NO')
	else:
		p = l.index(min(l))
		l5 = l[p:]
		l6 = l[:p]
		if l5 + l6 == l1:
			print('YES')
			print(1)
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(b, s) = (10000000000, l[0])
	l.append(b)
	c = 0
	for i in range(n):
		if l[i] > b:
			print('NO')
			break
		if l[i] > l[i + 1]:
			b = s
			c += 1
			s = l[i + 1]
	else:
		print('YES')
		print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	k = sorted(arr)
	if arr == k:
		print('YES')
		print(0)
		continue
	for i in range(0, n - 1):
		if arr[i + 1] < arr[i]:
			idx = i + 1
	new = arr[idx:n] + arr[0:idx]
	if new == k:
		print('YES')
		print(1)
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print('YES')
		print(0)
	else:
		for i in range(n - 1):
			if l[i] <= l[i + 1]:
				pass
			else:
				b = l[0:i + 1]
				c = l[i + 1:n + 1]
				break
		if c + b == sorted(l):
			print('YES')
			print(1)
		else:
			print('NO')


from sys import stdin, stdout, stderr, setrecursionlimit
from math import pi, sqrt, gcd, ceil, floor, log2, log10, factorial
from math import cos, acos, tan, atan, atan2, sin, asin, radians, degrees, hypot
from bisect import insort, insort_left, insort_right, bisect_left, bisect_right, bisect
from functools import reduce
from itertools import combinations, combinations_with_replacement, permutations
from fractions import Fraction
from random import choice, getrandbits, randint, random, randrange, shuffle
from re import compile, findall, escape, search, match
from statistics import mean, median, mode
from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge, nlargest, nsmallest
from collections import deque, OrderedDict, defaultdict
from collections import Counter, namedtuple, ChainMap, UserDict, UserList, UserString
import time
import math

def prime_number(start, end):
	l = []
	for num in range(start, end):
		if num > 1:
			for i in range(2, num):
				if num % i == 0:
					break
			else:
				l.append(num)
	return l

def findbin(a):
	return bin(a)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(n + 1):
		if prime[p]:
			print(p)

def primeornot(num):
	if num > 1:
		for i in range(2, num):
			if num % i == 0:
				print(num, 'is not a prime number')
				break
			else:
				print(num, 'is a prime number')
	else:
		print(num, 'is not a prime number')

def numSubarrayProductLessThanK(nums, k):
	if k <= 1:
		return 0
	prod = 1
	ans = left = 0
	for (right, val) in enumerate(nums):
		prod *= val
		while prod >= k:
			prod /= nums[left]
			left += 1
		ans += right - left + 1
	return ans
mod = 10 ** 9 + 7
lcm = lambda x, y: x * y // gcd(x, y)
add = lambda x, y: (x % mod + y % mod) % mod
sub = lambda x, y: (x % mod - y % mod + mod) % mod
mul = lambda x, y: x % mod * (y % mod) % mod
inverse = lambda x: pow(x, mod - 2, mod)
setBitCount = lambda x: bin(x).count('1')
sumOfDigits = lambda x: sum([int(i) for i in str(x)])
start_time = time.time()
for _ in range(int(input())):
	x = int(input())
	arr = list(map(int, input().split()))
	k = -1
	for i in range(1, x):
		if arr[i - 1] > arr[i]:
			k = i
			break
	else:
		print('YES')
		print(0)
	if k != -1:
		for i in range(k + 1, x):
			if arr[i - 1] > arr[i] or arr[i] > arr[0]:
				print('NO')
				break
		else:
			print('YES')
			print(1)
	end_time = time.time()


t = int(input())
for i in range(t):
	k = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print('YES')
		print(0)
	else:
		count = 0
		flag = 0
		for j in range(k - 1):
			if l[j] > l[j + 1]:
				count += 1
				if count > 1:
					print('NO')
					flag = 1
					break
		if count == 1 and l[0] >= l[k - 1]:
			print('YES')
			print(1)
		elif flag != 1:
			print('NO')


from collections import deque
t = int(input())
for i in range(t):
	n = int(input())
	l1 = []
	l1 = input().split()
	li = [int(q) for q in l1]
	index = 0
	flag1 = 0
	for w in range(n - 1):
		if li[w] <= li[w + 1]:
			continue
		else:
			flag1 = 1
			index = w
			break
	if flag1 == 1:
		li = deque(li)
		li.rotate(-(w + 1))
		li = list(li)
	flag = 0
	for e in range(n - 1):
		if li[e] <= li[e + 1]:
			flag = 0
		else:
			flag = 1
			break
	if flag == 1:
		print('NO')
	elif flag1 == 1:
		print('YES')
		print(1)
	else:
		print('YES')
		print(0)


for i in range(int(input())):
	N = int(input())
	s = list(map(int, input().split()))
	if s == sorted(s):
		print('YES')
		print(0)
	else:
		for i in range(0, N - 1):
			if s[i] > s[i + 1]:
				r = slice(i + 1, N)
				l = slice(0, i + 1)
				break
		f = s[r] + s[l]
		if f == sorted(s):
			print('YES')
			print(1)
		else:
			print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	j = n - 1
	x = []
	d = 0
	while j >= 0:
		c = j
		j -= 1
		while j >= 0 and l[j] <= l[j + 1]:
			j -= 1
		for b in range(j + 1, c + 1):
			x.append(l[b])
		d += 1
	if x == sorted(x):
		print('YES')
		print(d - 1)
	else:
		print('NO')


test = int(input())
while test:
	N = int(input())
	array = input()
	array = list(map(int, array.split()))
	brkFound = -1
	notPossible = False
	for i in range(1, N):
		if array[i] < array[i - 1]:
			brkFound = i
			break
	if brkFound != -1:
		if array[N - 1] > array[0]:
			notPossible = True
		for i in range(brkFound + 1, N):
			if array[i] < array[i - 1]:
				notPossible = True
	else:
		print('YES')
		print(0)
		test -= 1
		continue
	if notPossible:
		print('NO')
	else:
		print('YES')
		print(1)
	test -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print('YES')
		print('0')
	else:
		count = 0
		for j in range(n - 1):
			if l[j] > l[j + 1]:
				count += 1
		if count == 1 and l[n - 1] <= l[0]:
			print('YES')
			print('1')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	flag = 0
	for i in range(1, len(l)):
		if l[i] < l[i - 1]:
			l = l[i:] + l[:i]
			c += 1
		if c >= len(l):
			flag = 1
			break
	p = l.copy()
	p.sort()
	if p == l:
		print('YES')
		print(c)
	else:
		print('NO')


from sys import stdin, stdout
input = stdin.readline
import math
from collections import Counter, defaultdict
import bisect

def ti():
	return map(int, input().split(' '))

def tl():
	return list(map(int, input().split(' ')))
for _ in range(int(input())):
	n = int(input())
	a = tl()
	points = []
	start = 0
	end = 0
	for i in range(n - 1):
		if a[i] <= a[i + 1]:
			end = end + 1
		else:
			points.append([start, end])
			start = end = end + 1
	points.append([start, end])
	flag = 0
	for i in range(1, len(points)):
		if a[points[i][1]] > a[points[i - 1][0]]:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')
		print(len(points) - 1)


import sys
from collections import deque
from queue import PriorityQueue
from math import gcd
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
for _ in range(ii()):
	n = ii()
	a = il()
	if a == sorted(a):
		print('YES')
		print(0)
		continue
	p = 0
	fl = 0
	mn = a[0]
	for i in range(1, n):
		if p == 0:
			if a[i] < a[i - 1]:
				if mn < a[i]:
					fl = 1
					break
				p = 1
				continue
			else:
				mn = min(mn, a[i])
		elif a[i] > mn:
			fl = 1
			break
		elif a[i] < a[i - 1]:
			fl = 1
			break
	if fl == 0:
		print('YES')
		print(1)
	else:
		print('NO')


for u in range(int(input())):
	n = int(input())
	x = [int(w) for w in input().split()]
	if x == sorted(x):
		print('YES')
		print(0)
	else:
		f = 0
		for i in range(n - 1):
			if x[i] > x[i + 1]:
				f = i
				break
		y = x[i + 1:] + x[:i + 1]
		if y == sorted(x):
			print('YES')
			print(1)
		else:
			print('NO')


for i in range(int(input())):
	length = int(input())
	arr = list(map(int, input().split()))
	narr = sorted(arr)
	if arr == narr:
		print('YES')
		print(0)
	else:
		found = False
		for i in range(len(arr)):
			elem = arr.pop()
			arr.insert(0, elem)
			if arr == narr:
				found = True
				break
		if not found:
			print('NO')
		else:
			print('YES')
			print(1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr == sorted(arr):
		print('YES\n0')
		continue
	k = 0
	for i in range(n):
		if arr[i] > arr[i + 1]:
			k = i + 1
			break
	a1 = arr[0:k]
	a2 = arr[k:n]
	if a1 == sorted(a1) and a2 == sorted(a2) and (arr[n - 1] <= arr[0]):
		print('YES\n1')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sarr = sorted(arr)
	if arr == sarr:
		print('YES\n0')
	else:
		for i in range(n - 1):
			if arr[i] > arr[i + 1]:
				a = arr[:i + 1]
				b = arr[i + 1:]
				break
		if b + a == sarr:
			print('YES\n1')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	pre = float('inf')
	last = l[-1]
	for i in l[::-1]:
		if i > pre:
			c += 1
			if last > i:
				c = 2
				break
			else:
				last = i
		pre = i
		if c == 2:
			break
	if c == 2:
		print('NO')
	else:
		print('YES')
		print(c)


import sys

def getInput():
	return sys.stdin.readline().strip()

def getInt():
	return int(getInput())

def getInts():
	return map(int, getInput().split())

def getArray():
	return list(getInts())
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	sortli = sorted(li)
	count = 0
	for i in range(n - 1):
		if li[i] > li[i + 1]:
			count += 1
	if count == 0:
		print('YES')
		print(0)
	elif li[0] < li[-1]:
		print('NO')
	elif count == 1:
		print('YES')
		print(1)
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = sorted(arr)
	if arr1 == arr:
		print('YES')
		print(0)
	else:
		ct = 0
		arr.append(arr[0])
		for i in range(n):
			if arr[i] > arr[i + 1]:
				ct += 1
		if ct != 1:
			print('NO')
		else:
			print('YES')
			print(ct)


def check(lis):
	for i in range(len(lis) - 1):
		if lis[i + 1] < lis[i]:
			return False
	return True

def find(lis):
	i = len(lis) - 1
	while i > 0 and lis[i - 1] <= lis[i]:
		i -= 1
	return i
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	count = 0
	while not check(lis):
		if lis[0] < lis[-1]:
			break
		else:
			i = find(lis)
			lis = lis[i:] + lis[:i]
			count += 1
	if check(lis):
		print('YES')
		print(count)
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	sorr = list(map(int, input().split()))
	if sorr == sorted(sorr):
		print('YES\n0')
	else:
		for i in range(x - 1):
			if sorr[i] <= sorr[i + 1]:
				pass
			else:
				sec = sorr[0:i + 1]
				fir = sorr[i + 1:x + 1]
				break
		if fir + sec == sorted(sorr):
			print('YES\n1')
		else:
			print('NO')


from sys import stdin, stdout
import math, bisect
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	return ' '.join(list(map(str, a)))

def solve():
	n = I()
	a = L()
	b = 0
	for i in range(1, n):
		if a[i - 1] > a[i]:
			a = a[i:] + a[:i]
			b += 1
			break
	if a == sorted(a):
		print('YES')
		print(b)
	else:
		print('NO')
for _ in range(I()):
	solve()


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().strip().split()))
	count = 0
	mi = A[0]
	for i in range(1, N, 1):
		if A[i] < A[i - 1]:
			count += 1
		elif count > 0 and mi < A[i]:
			count = 2
			break
	if count < 2:
		print('YES')
		print(count)
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = min(l)
	x = 0
	for i in range(n - 1, -1, -1):
		if l[i] == k and l[i] < l[i - 1]:
			x = i
			break
	l1 = l[x:n] + l[0:x]
	f = 1
	for i in range(n - 1):
		if l1[i] > l1[i + 1]:
			f = 0
			break
	if not f:
		print('NO')
	else:
		print('YES')
		if x == 0:
			print(0)
		else:
			print(1)


def check(nums):
	decreased = False
	prior_num = nums[-1]
	for num in nums:
		if num < prior_num:
			if decreased:
				return False
			decreased = True
		prior_num = num
	return True
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if sorted(a) == a:
		print('YES')
		print('0')
	elif check(a):
		print('YES')
		print('1')
	else:
		print('NO')


import sys, math
from collections import defaultdict, deque
input = sys.stdin.readline
t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	sl = sorted(l)
	if sl == l:
		print('YES')
		print(0)
		t -= 1
		continue
	minval = float('inf')
	minidx = -1
	for i in range(1, n - 1):
		if l[i] > l[i + 1]:
			minidx = i + 1
			break
	if minidx == -1:
		print('NO')
	else:
		nl = [l[minidx]]
		mc = (minidx + 1) % n
		while mc != minidx:
			nl.append(l[mc])
			mc = (mc + 1) % n
		if nl == sl:
			print('YES')
			print(1)
		else:
			print('NO')
	t -= 1


def answer():
	if a == sorted(a):
		print('YES')
		print(0)
		return
	c = 0
	for i in range(n):
		if a[i % n] > a[(i + 1) % n]:
			if c == 1:
				print('NO')
				return
			c += 1
	print('YES')
	print(1)
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	answer()


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, trunc
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	n = iinp()
	arr = lmp()
	sarr = sorted(arr)
	flg = True
	for i in range(1, n):
		if arr[i] < arr[i - 1]:
			flg = False
			break
	if flg:
		print('YES', 0, sep='\n')
	elif arr[i:] + arr[:i] == sarr:
		print('YES', 1, sep='\n')
	else:
		print('NO')


from collections import deque

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	p = sorted(arr)
	fans = []
	ans = deque([arr[n - 1]])
	if arr == p:
		print('YES')
		print('0')
	else:
		l = 0
		moves = 0
		for x in range(n - 2, -1, -1):
			if arr[x] <= arr[x + 1]:
				ans.appendleft(arr[x])
			else:
				for z in list(ans):
					fans.append(z)
				moves += 1
				ans = deque([arr[x]])
		for z in list(ans):
			fans.append(z)
		for x in range(1, n):
			if fans[x] < fans[x - 1]:
				print('NO')
				return
			else:
				continue
		print('YES')
		print(moves)
for _ in range(int(input())):
	solve()


try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		l = [int(x) for x in input().split()]
		i = 0
		while i < n - 1 and l[i] <= l[i + 1]:
			i += 1
		if i == n - 1:
			print('YES')
			print(0)
			continue
		j = i + 1
		while j < n - 1 and l[j] <= l[j + 1]:
			j += 1
		if j == n - 1 and l[i + 1] <= l[0] and (l[n - 1] <= l[0]):
			print('YES')
			print(1)
			continue
		print('NO')
except EOFError:
	pass


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().strip().split()))[:n]
	lst2 = []
	flag = 0
	for i in range(n - 1):
		if lst[i] > lst[i + 1]:
			lst2 = lst[i + 1:] + lst[0:i + 1]
			flag = 1
			break
	if len(lst2) == 0:
		print('YES')
		print('0')
	elif flag == 1:
		for i in range(n - 1):
			if lst2[i] > lst2[i + 1]:
				print('NO')
				flag = 0
				break
		if flag == 1:
			print('YES')
			print('1')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print('YES')
		print(0)
	else:
		for i in range(1, n):
			if l[i] < l[i - 1]:
				x = slice(i, n + 1)
				a = slice(0, i)
				break
		b = l[x] + l[a]
		l.sort()
		if l == b:
			print('YES')
			print(1)
		else:
			print('NO')


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	p = -1
	flag = 1
	for i in range(1, n):
		if l[i] >= l[i - 1]:
			continue
		else:
			p = i
			break
	if p > -1:
		for i in range(p + 1, n):
			if l[i] >= l[i - 1]:
				continue
			else:
				flag = 0
				break
	if flag == 0:
		print('NO')
	elif p == -1:
		print('YES')
		print(0)
	elif l[n - 1] <= l[0]:
		print('YES')
		print(1)
	else:
		print('NO')
	t = t - 1


def suffix_sort(n, arr):
	inc1 = [arr[0]]
	inc2 = []
	for i in range(1, n):
		if arr[i] >= arr[i - 1]:
			inc1.append(arr[i])
		else:
			inc2 = arr[i:]
			break
	if len(inc2) == 0:
		print('YES')
		print('0')
	else:
		t = len(inc2)
		acc = 1
		for j in range(t - 1):
			if inc2[j] <= inc2[j + 1]:
				acc += 1
		if acc == t and inc2[-1] <= inc1[0]:
			print('YES')
			print('1')
		else:
			print('NO')
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	suffix_sort(n, arr)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 0
	val = max(arr)
	for i in range(len(arr) - 1):
		if arr[i] <= arr[i + 1] and arr[i + 1] <= val:
			pass
		else:
			val = arr[i]
			flag += 1
			if flag > 1:
				print('NO')
				break
	else:
		print('YES')
		print(flag)
	t -= 1


t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	count = 0
	flag = 0
	for i in range(0, n - 1):
		if arr[i] > arr[i + 1]:
			flag = 1
			break
	if flag == 0:
		print('YES')
		print('0')
	elif arr[n - 1] > arr[0]:
		print('NO')
	else:
		for i in range(0, n - 1):
			if arr[i] > arr[i + 1]:
				count += 1
		if count == 1:
			print('YES')
			print('1')
		else:
			print('NO')


T = int(input())
while T:
	N = int(input())
	A = list(map(int, input().split()))
	flag = 0
	f1 = 0
	count = 0
	for i in range(1, N):
		if A[i - 1] > A[i]:
			flag = 1
			if count == 0:
				count += 1
				continue
		if flag == 1:
			if A[i - 1] > A[i] or A[i] > A[0]:
				f1 = 1
				break
	if f1 == 1:
		print('NO')
	else:
		print('YES')
		if flag == 1:
			print(1)
		else:
			print(0)
	T -= 1


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	flag = True
	count = 0
	maxi = a[-1]
	for i in range(n - 1, 0, -1):
		if a[i] < a[i - 1]:
			if a[i - 1] >= maxi:
				maxi = a[i - 1]
				count += 1
			else:
				flag = False
				break
	if not flag:
		print('NO')
	else:
		print('YES')
		print(count)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	isincreasing = True
	i = 0
	for i in range(N - 1):
		if A[i + 1] < A[i]:
			isincreasing = False
			break
	if isincreasing == True:
		print('YES')
		print('0')
	else:
		isincreasing = True
		for i in range(i + 1, N - 1):
			if A[i + 1] < A[i]:
				isincreasing = False
				break
		if isincreasing and A[N - 1] <= A[0]:
			print('YES')
			print('1')
		else:
			print('NO')


t = int(input())
for h in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = 0
	c = 0
	for i in range(1, n):
		if a[i] < a[i - 1]:
			c = c + 1
			if c == 1:
				x = i
	if c == 0:
		print('YES\n0')
	elif c == 1:
		if x + 1 == n:
			print('YES\n1')
		elif a[x - 1] >= a[x + 1]:
			print('YES\n1')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ctr = 0
	ind = -1
	for j in range(n - 1):
		if s[j] > s[j + 1]:
			ctr += 1
			ind = j + 1
	if ctr >= 2:
		print('NO')
	elif ctr == 0:
		print('YES')
		print(ctr)
	else:
		k = sorted(s)
		d = ind
		d1 = 0
		while d < n:
			if k[d1] != s[d]:
				break
			else:
				d += 1
				d1 += 1
		if d == n:
			print('YES')
			print(1)
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 1
	ne = [0] * n
	ne[n - 1] = n - 1
	for i in range(n - 2, -1, -1):
		if a[i] <= a[i + 1]:
			ne[i] = ne[i + 1]
		else:
			ne[i] = i
	flag = 0
	tri = 9999999999999999999
	if ne[0] == n - 1:
		flag = 1
		tri = 0
	for i in range(1, n):
		if ne[i] == n - 1 and ne[0] >= i - 1 and (a[n - 1] <= a[0]):
			tri = min(tri, 1)
			flag = 1
	if flag == 1:
		print('YES')
		print(tri)
	else:
		print('NO')


t = int(input())
for i in range(t):
	(f, f1, c) = (0, 0, 0)
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(1, n):
		if l[i - 1] > l[i]:
			f = 1
			if c == 0:
				c += 1
				continue
		if f == 1:
			if l[i - 1] > l[i] or l[i] > l[0]:
				f1 = 1
				break
	if f1 == 1:
		print('NO')
	else:
		print('YES')
		if f == 1:
			print('1')
		else:
			print('0')