Everybody knows of spaghetti sort. You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you...

You come up with the following algorithm. For each number in the array a_{i}, build a stack of a_{i} ravioli. The image shows the stack for a_{i} = 4.

 [Image] 

Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed.

At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack.

Given an input array, figure out whether the described algorithm will sort it correctly.


-----Input-----

The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array.

The second line of input contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 100) — the elements of the array.


-----Output-----

Output "YES" if the array can be sorted using the described procedure and "NO" if it can not.


-----Examples-----
Input
3
1 2 3

Output
YES

Input
3
3 1 2

Output
NO



-----Note-----

In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.

Difficulty: MEDIUM_HARD
Tags: Implementation

Starter code:

Solutions:
import math
n = int(input())
s = input().split()
for i in range(1, len(s)):
	if math.fabs(int(s[i]) - int(s[i - 1])) > 1:
		print('NO')
		return
print('YES')


N = int(input())
arr = list(map(int, input().split()))
out = []

def check(l):
	i = 1
	while i < len(l):
		if abs(l[i] - l[i - 1]) >= 2:
			return False
		i += 1
	return True
if not check(arr) or not check(out):
	print('NO')
	exit(0)
for _ in range(len(arr)):
	maxIndex = 0
	for i in range(len(arr)):
		if arr[maxIndex] < arr[i]:
			maxIndex = i
	out.append(arr[maxIndex])
	del arr[maxIndex]
	if not check(arr) or not check(out):
		print('NO')
		exit(0)
print('YES')


n = int(input())
a = list(map(int, input().split()))
for i in range(1, n):
	if abs(a[i - 1] - a[i]) >= 2:
		print('NO')
		break
else:
	print('YES')


def sign(x):
	if x > 0:
		return 1
	elif x == 0:
		return 0
	return -1

def bober(a, b):
	return sign(a - b) * (a - b)
n = int(input())
a = list(map(int, input().split()))
mx = 0
for i in range(1, n):
	mx = max(mx, bober(a[i], a[i - 1]))
if mx <= 1:
	print('YES')
else:
	print('NO')


n = int(input())
t = list(map(int, input().split()))
flag = True
while len(t) > 1 and flag:
	nn = max(t)
	for i in range(0, len(t)):
		if t[i] == nn:
			if i + 1 < len(t) and t[i] - t[i + 1] >= 2 or (i - 1 >= 0 and t[i] - t[i - 1] >= 2):
				flag = False
	t.remove(nn)
if flag:
	print('yes')
else:
	print('no')


n = input()
array = input().split()
array = list(map(int, array))
original = array.copy()

def raviolisort(array):
	out = []
	while len(array) > 0:
		for i in range(1, len(array)):
			if array[i] - array[i - 1] >= 2:
				array[i] -= 1
				array[i - 1] += 1
			elif array[i - 1] - array[i] >= 2:
				array[i] += 1
				array[i - 1] -= 1
		maximal = array[0]
		maximali = 0
		for i in range(1, len(array)):
			if array[i] > maximal:
				maximal = array[i]
				maximali = i
		del array[maximali]
		out.append(maximal)
	return out
out = raviolisort(array)[::-1]
if out == sorted(original):
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
for i in range(len(a) - 1):
	if abs(a[i + 1] - a[i]) > 1:
		print('NO')
		return
print('YES')


n = int(input())
a = list(map(int, input().split()))
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) >= 2:
		print('NO')
		exit(0)
print('YES')


n = int(input())
a = list(map(int, input().split()))
for i in range(1, n):
	if a[i] - a[i - 1] > 1 or a[i - 1] - a[i] > 1:
		print('NO')
		exit()
print('YES')


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


input()
a = list(map(int, input().split(' ')))
k = 0
while len(a) > 0:
	b = a.index(max(a))
	try:
		if abs(a[b] - a[b + 1]) >= 2:
			k = 1
			break
	except IndexError:
		pass
	try:
		if abs(a[b] - a[abs(b - 1)]) >= 2:
			k = 1
			break
	except IndexError:
		pass
	a.pop(b)
if k == 1:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split(' ')))

def check(a):
	for i in range(1, len(a)):
		if abs(a[i] - a[i - 1]) > 1:
			return False
	return True

def make_next(a):
	a.remove(max(a))
while len(a) > 0:
	if not check(a):
		print('NO')
		exit()
	make_next(a)
print('Yes')


i = int(input())
n = list(map(int, input().split()))
f = True
for j in range(i - 1):
	if -1 > n[j] - n[j + 1] or n[j] - n[j + 1] > 1:
		print('No')
		f = False
		break
if f:
	print('Yes')


def check(a):
	for i in range(len(a) - 1):
		if abs(a[i] - a[i + 1]) > 1:
			return False
	return True
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	if not check(a):
		print('NO')
		break
	ind = a.index(max(a))
	b.append(a.pop(ind))
else:
	print('YES')


input()
a = [int(x) for x in input().split()]
while len(a):
	tmp = -1
	for x in a:
		if tmp == -1:
			tmp = x
			continue
		if abs(x - tmp) > 1:
			print('NO')
			quit()
		tmp = x
	a.remove(max(a))
print('YES')


ooo = input()
m = [int(i) for i in input().split()]
k = False
for i in range(int(ooo) - 1):
	if abs(m[i] - m[i + 1]) > 1:
		print('NO')
		k = True
		break
if not k:
	print('YES')


import sys

def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	if n == 1:
		print('YES')
	else:
		flag = 1
		while len(a) > 1:
			Len = len(a)
			for i in range(0, Len - 1):
				if a[i] - a[i + 1] > 1 or a[i + 1] - a[i] > 1:
					flag = 0
			if flag == 0:
				break
			idx = max(a)
			now = a.index(idx)
			a.pop(now)
		if flag == 1:
			print('YES')
		else:
			print('NO')
main()


firstline = input().strip().split(' ')
n = int(firstline[0])
theArray = input().strip().split(' ')
theArray = list(map(int, theArray))

def hasSlidingRavioli(someArray):
	for i in range(len(someArray) - 1):
		if abs(someArray[i] - someArray[i + 1]) >= 2:
			return True
	return False
if n == 1:
	print('YES')
	exit()
elif hasSlidingRavioli(theArray):
	print('NO')
	exit()
else:
	for i in range(n):
		theArray.remove(max(theArray))
		if hasSlidingRavioli(theArray):
			print('NO')
			exit()
	print('YES')


def check(a):
	e = 0
	for i in range(len(a) - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			e = 1
			break
	return e
n = int(input())
a = list(map(int, input().split()))
while len(a) > 0:
	b = check(a)
	if b == 1:
		break
	a.remove(max(a))
if b == 1:
	print('NO')
else:
	print('YES')


def fa(p):
	for i in range(1, len(p)):
		dd = p[i] - p[i - 1]
		if dd < 0:
			dd = -dd
		if dd >= 2:
			return 1
	return 0
n = int(input())
a = list(map(int, input().split()))
b = []
flag = 1
if fa(a):
	flag = 0
for i in range(n):
	c = []
	k = 0
	for j in range(len(a)):
		if a[j] > a[k]:
			k = j
	b.append(a[k])
	for j in range(len(a)):
		if j != k:
			c.append(a[j])
	a = c
	if fa(a):
		flag = 0
	if fa(b):
		flag = 0
if flag:
	print('YES')
else:
	print('NO')


from math import fabs
n = int(input())
flag = True
x = list(map(int, input().split()))
for i in range(len(x) - 1):
	if abs(x[i] - x[i + 1]) >= 2:
		print('NO')
		flag = False
		break
if flag:
	print('YES')


n = input()
a = [int(x) for x in input().split()]
f = lambda x: all((abs(a - b) < 2 for (a, b) in zip(a, a[1:])))
while a:
	if not f(a):
		print('NO')
		exit(0)
	m = max(a)
	a = [x for x in a if x != m]
print('YES')


n = int(input())
q = [int(x) for x in input().split()]
for i in range(n - 1):
	if abs(q[i] - q[i + 1]) > 1:
		print('NO')
		break
else:
	print('YES')


a = int(input())
A = list(map(int, input().split()))
b = True
for i in range(a - 1):
	if abs(A[i + 1] - A[i]) > 1:
		b = False
		break
if b:
	print('YES')
else:
	print('NO')


n = int(input())
b = True
a = [int(i) for i in input().split()]
for i in range(n - 1):
	if abs(a[i + 1] - a[i]) >= 2:
		b = False
if b:
	print('YES')
else:
	print('NO')


from sys import exit
a = int(input())
arr = input().split()
prev = None
for i in range(a):
	x = int(arr[i])
	if prev is not None and abs(x - prev) > 1:
		print('NO')
		exit()
	prev = x
print('YES')


n = int(input())
a = input().strip().split()
a = [int(i) for i in a]
for i in range(0, n):
	for j in range(i + 1, n):
		if abs(a[i] - a[j]) >= 2:
			if j == i + 1:
				print('NO')
				exit(0)
			else:
				count = 0
				for k in range(i + 1, j):
					if a[k] > a[j] and a[k] > a[i]:
						count += 1
				if count == j - i - 1:
					print('NO')
					exit(0)
print('YES')


import sys
n = int(input())
(*a,) = map(int, input().split())
for i in range(n - 1):
	if a[i] > a[i + 1] + 1:
		print('NO')
		sys.exit()
print('YES')


n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = []
for i in range(n):
	for j in range(n - 1 - i):
		while a[j] - a[j + 1] >= 2:
			a[j] -= 1
			a[j + 1] += 1
		while a[j + 1] - a[j] >= 2:
			a[j + 1] -= 1
			a[j] += 1
	k = a.index(max(a))
	c.append(max(a))
	a = a[:k] + a[k + 1:]
if c[::-1] == b:
	print('YES')
else:
	print('NO')


N = int(input())
A = list(map(int, input().split(' ')))
if any((abs(i - j) >= 2 for (i, j) in zip(A, A[1:]))):
	print('NO')
else:
	print('YES')


n = int(input())
qwe = False
tmp = [int(x) for x in input().split()]
asd = [i for i in range(n)]
for i in range(n):
	max = 0
	k = 0
	for j in asd:
		if j != n - 1 and abs(tmp[j] - tmp[j + 1]) >= 2:
			qwe = True
	for j in asd:
		if max < tmp[j]:
			max = tmp[j]
			k = j
	asd.remove(k)
if qwe:
	print('NO')
else:
	print('YES')


TN = 1

def solution():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'YES'
	for i in range(n - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			ans = 'NO'
	print(ans)
while TN != 0:
	solution()
	TN -= 1


import re
import math
import decimal
import bisect

def read():
	return input().strip()
if int(read()) == 1:
	print('YES')
	exit()
nums = [int(x) for x in read().split(' ')]
for i in range(1, len(nums)):
	if abs(nums[i - 1] - nums[i]) > 1:
		print('NO')
		exit()
print('YES')


n = int(input())
ar = [int(x) for x in input().split()]
o = []

def slide(ar):
	for i in range(len(ar) - 1):
		if abs(ar[i] - ar[i + 1]) >= 2:
			return True

def solve(ar):
	while ar:
		if slide(ar):
			print('NO')
			return
		i = ar.index(max(ar))
		del ar[i]
	print('YES')
solve(ar)


n = int(input())
a = [int(i) for i in input().split()]
j = 0
t = 'YES'
while j < n - 1:
	if a[j] - a[j + 1] > 1 or a[j] - a[j + 1] < -1:
		t = 'NO'
		break
	j += 1
print(t)


n = int(input())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
c = []
for i in range(n):
	for j in range(len(a) - 1):
		if a[j] > a[j + 1] + 1 or a[j] < a[j + 1] - 1:
			print('NO')
			exit(0)
	c += [max(a)]
	a.remove(max(a))
assert b == c, (b, c)
print('YES')


import sys
sys.stdin.readline()
l = list(map(int, sys.stdin.readline().split()))

def is_bad(l):
	for (a, b) in zip(l, l[1:]):
		if abs(a - b) >= 2:
			return True
	return False
while len(l) > 0:
	if is_bad(l):
		print('NO')
		break
	idx = -1
	val = -1
	for (j, v) in enumerate(l):
		if v > val:
			idx = j
			val = v
	del l[idx]
else:
	print('YES')


in_count = int(input())
in_str = input()
a = [int(x) for x in in_str.split()[:in_count]]
a_sorted = a[:]
a_sorted.sort(reverse=True)
a_res = []
while len(a):
	for idx in range(0, len(a) - 2):
		if a[idx] - a[idx + 1] >= 2:
			a[idx] -= 1
			a[idx + 1] += 1
		elif a[idx + 1] - a[idx] >= 2:
			a[idx] += 1
			a[idx + 1] -= 1
	a_max = max(a)
	a.remove(a_max)
	a_res.append(a_max)
if a_sorted == a_res:
	print('YES')
else:
	print('NO')


print('NO' if (lambda n, a: any((abs(a[i + 1] - a[i]) > 1 for i in range(n - 1))))(int(input()), list(map(int, input().split()))) else 'YES')


n = int(input())
a = list(map(int, input().split()))
ans = True
for i in range(1, n):
	if abs(a[i] - a[i - 1]) > 1:
		ans = False
if ans:
	print('YES')
else:
	print('NO')


n = int(input())
L = [int(i) for i in input().split()]
for (a, b) in zip(L, L[1:]):
	if abs(a - b) > 1:
		print('NO')
		exit()
print('YES')


input()
a = [int(x) for x in input().split()]
while a:
	for i in range(len(a) - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			print('NO')
			exit(0)
	i = a.index(max(a))
	a.pop(i)
print('YES')


n = int(input())
a = list(map(int, input().split()))
for i in range(1, n):
	if abs(a[i] - a[i - 1]) > 1:
		print('NO')
		exit(0)
a.sort()
b = [a[0]]
for i in range(1, n):
	if a[i] != a[i - 1]:
		b.append(a[i])
for i in range(len(b)):
	if b[i] != i + b[0]:
		print('NO')
		exit(0)
print('YES')


try:
	n = int(input())
	a = list(map(int, input().split()))
except:
	print('NO')
	exit()
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) > 1:
		print('NO')
		exit()
print('YES')


def flat(b):
	i = 0
	while i < len(b) - 1:
		if b[i] - b[i + 1] >= 2:
			b[i] -= 1
			b[i + 1] += 1
			i = 0
		elif b[i] - b[i + 1] <= -2:
			b[i] += 1
			b[i + 1] -= 1
			i = 0
		else:
			i += 1
	return b
n = int(input())
a = [int(i) for i in input().split(' ')]
a1 = sorted(a)
a2 = []
while len(a) > 0:
	a = flat(a)
	m = max(a)
	a2 = [m] + a2
	a.remove(m)
if a1 == a2:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split(' ')))
isok = 1
for i in range(n - 1):
	isok = isok & (-1 <= a[i + 1] - a[i] and a[i + 1] - a[i] <= 1)
if isok:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(x) for x in input().split()]
while len(a):
	max = a[0]
	pos = 0
	for i in range(1, len(a)):
		if abs(a[i - 1] - a[i]) >= 2:
			pos = -1
			break
		if max < a[i]:
			max = a[i]
			pos = i
	if pos == -1:
		break
	a.pop(pos)
if len(a):
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
a += (a[-1],)
b = [abs(a[i] - a[i - 1]) for i in range(1, n + 1)]
print('YNEOS'[max(b) > 1::2])


_ = input()
l = list(map(int, input().split()))
ans = True
for i in range(1, len(l)):
	if abs(l[i] - l[i - 1]) >= 2:
		ans = False
		break
if ans:
	print('YES')
else:
	print('NO')


def check(a):
	for i in range(len(a) - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			return False
	return True
n = int(input())
a = list(map(int, input().split()))
ans = 'YES'
while a:
	if not check(a):
		ans = 'NO'
		break
	ind = 0
	mx = a[0]
	for i in range(1, n):
		if a[i] > mx:
			mx = a[i]
			ind = i
	n -= 1
	del a[ind]
print(ans)


n = int(input())
l = list(map(int, input().split()))
while len(l):
	for i in range(len(l) - 1):
		if abs(l[i] - l[i + 1]) > 1:
			exit(print('no'))
	l.remove(max(l))
print('yes')


n = int(input())
ar = list(map(int, input().split(' ')))
cur = ar[0]
for i in ar:
	if abs(cur - i) >= 2:
		print('NO')
		exit(0)
	cur = i
print('YES')


n = int(input())
a = list(map(int, input().split()))
q = []
flag = 0
for i in range(n):
	z = -1
	ma = 0
	for j in range(len(a) - 1):
		if abs(a[j] - a[j + 1]) >= 2:
			flag = 1
		if a[j] > ma:
			ma = a[j]
			z = j
	if a[len(a) - 1] > ma:
		ma = a[len(a) - 1]
		z = len(a) - 1
	q += [ma]
	a = a[:z] + a[z + 1:]
if flag == 0:
	print('YES')
else:
	print('NO')


n = int(input())
r = list(map(int, input().split(' ')))
s = []

def ok():
	prev = r[0]
	for ra in r[1:]:
		if abs(ra - prev) >= 2:
			return False
		prev = ra
	return True
while len(r):
	if not ok():
		print('NO')
		exit(0)
	maxi = 0
	for i in range(len(r)):
		if r[i] > r[maxi]:
			maxi = i
	r.pop(maxi)
print('YES')


n = int(input())
a = list(map(int, input().split()))
ch = 1
for i in range(1, n):
	if abs(a[i] - a[i - 1]) > 1:
		ch = 0
if ch == 1:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
b = list(sorted(a, reverse=True))
c = []
for i in range(n):
	for j in range(n - i - 1):
		if abs(a[j] - a[j + 1]) >= 2:
			print('NO')
			exit()
	q = max(a)
	qi = a.index(q)
	c.append(q)
	for j in range(i):
		if abs(c[j] - c[j + 1]) >= 2:
			print('NO')
			exit()
	del a[qi]
print('YES')


n = int(input())
s = [int(k) for k in input().split()]
i = 1
while i < n:
	if abs(s[i] - s[i - 1]) < 2:
		i += 1
	else:
		break
if i == len(s):
	print('YES')
else:
	print('NO')


input()
xs = list(map(int, input().split()))
for i in range(len(xs) - 1):
	if abs(xs[i] - xs[i + 1]) > 1:
		print('NO')
		exit()
print('YES')


n = int(input())
A = [int(x) for x in input().split()]

def fuck(A):
	for i in range(1, len(A)):
		if abs(A[i] - A[i - 1]) > 1:
			return True
	return False
ok = True
while A != sorted(A):
	if fuck(A):
		ok = False
		break
	a = max(A)
	A.remove(a)
if ok:
	print('YES')
else:
	print('NO')


def rav_sorted(a):
	while len(a) >= 1:
		if len(a) == 1:
			answer.insert(0, max(a))
			a.pop(a.index(max(a)))
		elif a.index(max(a)) == 0:
			if a[a.index(max(a))] - a[a.index(max(a)) + 1] >= 2:
				a[0] -= 1
				a[1] += 1
			else:
				answer.insert(0, max(a))
				a.pop(a.index(max(a)))
		elif a.index(max(a)) == len(a) - 1:
			if a[a.index(max(a))] - a[a.index(max(a)) - 1] >= 2:
				a[len(a) - 1] -= 1
				a[len(a) - 2] += 1
			else:
				answer.insert(0, max(a))
				a.pop(a.index(max(a)))
		elif a[a.index(max(a))] - a[a.index(max(a)) - 1] >= 2 and a[a.index(max(a))] - a[a.index(max(a)) + 1] >= 2:
			a[a.index(min(a[a.index(max(a)) - 1], a[a.index(max(a)) + 1]))] += 1
			a[a.index(max(a))] -= 1
		else:
			answer.insert(0, max(a))
			a.pop(a.index(max(a)))
	return answer
answer = []
n = int(input())
inp = list(map(int, input().split()))
a = sorted(inp)
if rav_sorted(inp) == a:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
i = 1
s = True
while i < n:
	d = abs(a[i] - a[i - 1])
	if d >= 2:
		s = False
		break
	i += 1
if s:
	print('YES')
else:
	print('NO')


def check_array(arr):
	for (el1, el2) in zip(arr[1:], arr[:-1]):
		if abs(el1 - el2) >= 2:
			return False
	return True
n = int(input())
a = [int(_) for _ in input().split()]
res = []
if not check_array(a):
	print('NO')
else:
	while a:
		res.append(max(a))
		a.remove(max(a))
		if not check_array(a) or not check_array(res):
			print('NO')
	print('YES')


n = int(input())
a = list(map(int, input().split()))
ans = 'YES'
for i in range(n - 1):
	m = max(a)
	for j in range(n - i - 1):
		if abs(a[j + 1] - a[j]) > 1:
			ans = 'NO'
	a.remove(m)
print(ans)


n = int(input())
a = list(map(int, input().split()))
while len(a):
	for i in range(1, len(a)):
		if abs(a[i] - a[i - 1]) >= 2:
			print('NO')
			exit(0)
	del a[a.index(max(a))]
print('YES')


n = int(input())
a = [int(i) for i in input().split(' ')]
ans = True
while len(a):
	for i in range(len(a) - 1):
		if abs(a[i + 1] - a[i]) >= 2:
			ans = False
			break
	if not ans:
		break
	m = max(a)
	for i in range(len(a)):
		if a[i] == m:
			a.pop(i)
			break
print('YES' if ans else 'NO')


n = int(input())
a = list(map(int, input().split()))
s = set()
for i in range(n - 1):
	s.add(abs(a[i] - a[i + 1]))
if len(list(filter(lambda x: x > 1, s))) > 0:
	print('NO')
else:
	print('YES')


def main():
	n = input()
	a = list(map(int, input().split()))
	for i in range(len(a) - 1):
		if a[i + 1] - a[i] >= 2 or a[i + 1] - a[i] <= -2:
			print('NO')
			return
	print('YES')
main()


def initialize_solution():
	pass

def main():
	(n,) = read()
	a = read()
	for i in range(n - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			return 'NO'
	return 'YES'
READ_FROM_FILE = None
OUTPUT_PREFIX = None
INTERACTIVE = False
import sys

def read(callback=int, split=True):
	if READ_FROM_FILE:
		ipt = sfile.readline().strip()
	else:
		ipt = input().strip()
	if INTERACTIVE and ipt == 'WRONG_ANSWER':
		sys.exit()
	if split:
		return list(map(callback, ipt.split()))
	else:
		return callback(ipt)

def write(value, end='\n'):
	if value is None:
		return
	try:
		if not isinstance(value, str):
			value = ' '.join(map(str, value))
	except:
		pass
	if READ_FROM_FILE:
		tfile.write(str(value, end=end))
	else:
		print(value, end=end)
	if INTERACTIVE:
		sys.stdout.flush()
sfile = None
tfile = None
if READ_FROM_FILE:
	sfile = open(READ_FROM_FILE + '.in', 'r')
	sfile.seek(0)
	tfile = open(READ_FROM_FILE + '.out', 'w')
if OUTPUT_PREFIX is None:
	result = main()
	if result is not None:
		write(result)
else:
	initialize_solution()
	(TOTAL_CASES,) = read()
	for CASE_NUMBER in range(1, TOTAL_CASES + 1):
		write(OUTPUT_PREFIX.replace('%d', str(CASE_NUMBER)), end='')
		result = main()
		if result is not None:
			write(result)
if sfile is not None:
	sfile.close()
if tfile is not None:
	tfile.close()


n = int(input())
ns = list(map(int, input().split()))
while ns:
	for i in range(1, len(ns)):
		d = abs(ns[i - 1] - ns[i])
		if d > 1:
			print('NO')
			quit()
	m = max(ns)
	for i in range(len(ns)):
		if ns[i] == m:
			ns.pop(i)
			break
print('YES')


n = int(input())
a = list(map(int, input().split()))
r = 'YES'
for i in range(1, n):
	if abs(a[i] - a[i - 1]) >= 2:
		r = 'NO'
		break
if r == 'NO':
	print(r)
else:
	h = max(a)
	while len(a) > 0:
		k = max(a)
		j = a.index(k)
		a.pop(j)
		if abs(k - h) >= 2:
			r = 'NO'
			break
		for i in range(1, len(a)):
			if abs(a[i] - a[i - 1]) >= 2:
				r = 'NO'
				break
		h = k
	print(r)


from math import sin, cos, pi, fabs, sqrt, log2
import os, sys
n = int(input())
arr = list(map(int, input().split(' ')))
for i in range(len(arr) - 1):
	if abs(arr[i] - arr[i + 1]) > 1:
		print('NO')
		sys.exit()
print('YES')


n = int(input())
a = list(map(int, input().split()))
for i in range(len(a) - 1):
	if abs(a[i + 1] - a[i]) > 1:
		print('NO')
		exit(0)
while len(a) > 0:
	m = max(a)
	i = a.index(m)
	a.remove(m)
	if 0 < i < len(a) and abs(a[i] - a[i - 1]) > 1:
		print('NO')
		exit(0)
print('YES')


n = int(input())
f = 0
l = list(map(int, input().split()))
if n == 1:
	print('YES')
else:
	for i in range(1, n):
		if abs(l[i] - l[i - 1]) >= 2:
			f = 1
			break
	if f == 1:
		print('NO')
	else:
		print('YES')


n = int(input())
l = list(map(int, input().split()))
bad = False
while len(l) > 0:
	bad = False
	for i in range(len(l) - 1):
		if abs(l[i] - l[i + 1]) >= 2:
			bad = True
			break
	if bad:
		print('NO')
		break
	else:
		p = max(l)
		l.remove(p)
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
ans2 = sorted(a)
ans = []
for i in range(n):
	for i in range(10000):
		for j in range(1, len(a)):
			if abs(a[j] - a[j - 1]) > 1:
				if a[j] > a[j - 1]:
					a[j] -= 1
					a[j - 1] += 1
				else:
					a[j] += 1
					a[j - 1] -= 1
	max_x = a[0]
	i_x = 0
	for j in range(len(a)):
		if a[j] > max_x:
			max_x = a[j]
			i_x = j
	ans.append(max_x)
	a.pop(i_x)
ans.reverse()
if ans == ans2:
	print('YES')
else:
	print('NO')


n = int(input())
num = input().split()
flag = 1
for i in range(n - 1):
	if abs(int(num[i]) - int(num[i + 1])) >= 2:
		flag = 0
if flag == 1:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(x) for x in input().split()]

def upd(a):
	fl = True
	while fl:
		fl = False
		for i in range(len(a)):
			fl1 = i != 0 and a[i - 1] <= a[i] - 2
			fl2 = i < len(a) - 1 and a[i + 1] <= a[i] - 2
			pos = i - 1 if fl1 and fl2 and (a[i - 1] < a[i + 1]) or (fl1 and (not fl2)) else i + 1 if fl2 else -1
			if pos == -1:
				continue
			a[pos] += 1
			a[i] -= 1
	return a
b = a[:]
b.sort()
a = upd(a)
res = []
while len(a) > 0:
	pos = -1
	val = 0
	for i in a:
		val = max(val, i)
	for i in range(len(a)):
		if a[i] == val:
			res.append(a[i])
			a = a[:i] + a[i + 1:len(a)]
			break
	a = upd(a)
res = res[::-1]
print('YES' if res == b else 'NO')


n = int(input())
s = input().split()
arr = [int(i) for i in s]
for i in range(n - 1):
	if arr[i] - arr[i + 1] >= 2 or arr[i + 1] - arr[i] > 2:
		print('NO')
		exit(0)
print('YES')


n = int(input())
a = [int(n) for n in input().split()]
answer = True
if n != 1:
	for i in range(0, n - 1):
		if max(a[i], a[i + 1]) - min(a[i + 1], a[i]) >= 2:
			answer = False
			break
	if answer:
		print('YES')
	else:
		print('NO')
else:
	print('YES')


def govno():
	for i in range(len(num) - 1):
		if abs(num[i] - num[i + 1]) >= 2:
			if num[i] < num[i + 1]:
				num[i + 1] -= 1
				num[i] += 1
			else:
				num[i] -= 1
				num[i + 1] += 1
n = int(input())
num = list(map(int, input().split()))
cor = []
cor += num
cor.sort()
our = []
for i in range(n):
	govno()
	a = max(num)
	num.pop(num.index(a))
	our.append(a)
our.reverse()
if our == cor:
	print('YES')
else:
	print('NO')


n = int(input())
l = [int(s) for s in input().split()]
z = 0
f = 0
d = 0
e = 0
while len(l) > 0:
	a = max(l)
	if l.index(a) != 0:
		d = a - l[l.index(a) - 1]
	if l.index(a) != len(l) - 1:
		e = a - l[l.index(a) + 1]
	if d >= 2:
		z = 1
		break
	if e >= 2:
		f = 1
		break
	else:
		l.remove(a)
if z == 1 or f == 1:
	print('NO')
else:
	print('YES')


def main():
	n = int(input())
	l = list(map(int, input().strip().split()))
	count = 0
	while l != []:
		for i in range(n - 1 - count):
			if abs(l[i] - l[i + 1]) > 1:
				print('NO')
				return
		del l[l.index(max(l))]
		count = count + 1
	print('YES')
main()


n = int(input())
a = [int(i) for i in input().split()]
ans = 'YES'
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) > 1:
		ans = 'NO'
print(ans)


def ravioli(array):
	for x in range(1, len(array), 2):
		dif = abs(array[x] - array[x - 1])
		if dif >= 2:
			print('NO')
			exit(0)
			if array[x] > array[x - 1]:
				dif = -dif
			array[x] += dif - 1
			array[x - 1] -= dif - 1
pos = -1
n = int(input())
a = list(map(int, input().split()))
orig = sorted(a)
b = []
while len(a) != 0:
	mx = 0
	pos = -1
	ravioli(a)
	for x in range(len(a)):
		if a[x] > mx:
			mx = a[x]
			pos = x
	if pos != -1:
		b.insert(0, a.pop(pos))
result = 'NO'
if b == orig:
	result = 'YES'
print(result)


n = int(input())
a = [int(i) for i in input().split()]
while len(a) != 1:
	m = a[0]
	im = 0
	for i in range(1, len(a)):
		if abs(a[i] - a[i - 1]) >= 2:
			print('NO')
			exit()
		if a[i] > m:
			m = a[i]
			im = i
	a.pop(im)
print('YES')


import sys
(output, arr) = ([], [])
n = int(input())
arr = list(map(int, sys.stdin.readline().strip().split(' ')))
arr.append(-10)

def ravioli(alist):
	temp = max(alist)
	output.append(temp)
	del alist[alist.index(temp)]

def main():
	for i in range(n - 1):
		if arr[i + 1] > 0 and abs(arr[i] - arr[i + 1]) >= 2:
			return 'NO'
	for j in range(n):
		ravioli(arr)
	return 'YES'
print(main())


size = int(input())
data = [int(x) for x in input().split()]
error = False
while size > 1:
	for i in range(size - 1):
		if abs(data[i] - data[i + 1]) >= 2:
			error = True
	data.remove(max(data))
	size -= 1
print('NO' if error else 'YES')


n = int(input())
a = list(map(int, input().split()))
a_good = sorted(a)

def fall(a):
	n = len(a)
	for i in range(n):
		if i < n - 1 and a[i] - a[i + 1] >= 2:
			a[i] -= 1
			a[i + 1] += 1
		if i > 0 and a[i] - a[i - 1] >= 2:
			a[i] -= 1
			a[i - 1] += 1

def ravioli(a):
	result = list()
	while len(a):
		fall(a)
		result = [max(a)] + result
		idx = a.index(max(a))
		a.pop(idx)
	return result
a_rav = ravioli(a)
if a_rav == a_good:
	print('YES')
else:
	print('NO')


def rav(arr):
	for i in range(len(arr) - 1):
		if abs(arr[i] - arr[i + 1]) < 2:
			continue
		else:
			return 'NO'
	return 'YES'
n = int(input())
arr1 = list(map(int, input().strip().split()))
print(rav(arr1))


import itertools
from collections import deque

def pairwise(iterable):
	(a, b) = itertools.tee(iterable)
	next(b, None)
	return zip(a, b)
count = input()
numbers = [int(x) for x in input().split()]
res = deque()
while numbers:
	if any((True for (a, b) in pairwise(numbers) if abs(a - b) >= 2)):
		print('NO')
		break
	max_num = -1
	max_index = -1
	for (i, num) in enumerate(numbers):
		if num > max_num:
			max_index = i
			max_num = num
	res.appendleft(numbers.pop(max_index))
else:
	print('YES')


n = int(input())
A = list(map(int, input().split()))
print('YES' if all((abs(a - b) <= 1 for (a, b) in zip(A, A[1:]))) else 'NO')


def oviolisort(arr1):
	for i in range(len(arr1) - 1):
		if abs(arr1[i + 1] - arr1[i]) >= 2:
			return False
	return True
n = int(input())
arr1 = list(map(int, input().split()))
if oviolisort(arr1):
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(i) for i in input().split()]
b = [abs(a[i + 1] - a[i]) for i in range(n - 1)]
f = 1
for i in b:
	if i >= 2:
		f = 0
		break
if f:
	print('YES')
else:
	print('NO')


import sys
n = int(sys.stdin.readline().strip())
ais = list(map(int, sys.stdin.readline().strip().split()))

def good(a):
	for i in range(len(a) - 1):
		if abs(a[i] - a[i + 1]) > 1:
			return False
	return True
while ais:
	if not good(ais):
		print('NO')
		break
	am = max(ais)
	iam = ais.index(am)
	del ais[iam]
	if ais == []:
		print('YES')
		break


n = int(input())
a = list(map(int, input().split(' ')))
st = 'YES'
k = a[0]
for i in a:
	if abs(k - i) >= 2:
		st = 'NO'
	k = i
print(st)


def is_sort_correct(n, a):
	for _ in range(n):
		max_indx = a.index(max(a))
		if max_indx + 1 != len(a):
			if abs(a[max_indx] - a[max_indx + 1]) > 1:
				return False
		if max_indx != 0:
			if abs(a[max_indx] - a[max_indx - 1]) > 1:
				return False
		a.pop(max_indx)
	return True
n = int(input())
a = [int(ai) for ai in input().split()]
print('YES' if is_sort_correct(n, a) else 'NO')


N = int(input())
numbers = list(map(int, input().split()))
failed = False
for i in range(1, N - 1):
	if abs(numbers[i - 1] - numbers[i]) > 1 or abs(numbers[i + 1] - numbers[i]) > 1:
		failed = True
		break
if failed:
	print('NO')
else:
	print('YES')


n = int(input())
myList = list(map(int, input().split()))
myNewList = myList[:]
myNewList.sort()
myThirdList = []
for i in range(0, n - 1):
	if myList[i] - myList[i + 1] >= 2:
		myList[i] = myList[i] - 1
		myList[i + 1] = myList[i + 1] + 1
myList.sort()
if myList == myNewList:
	print('YES')
else:
	print('NO')


def main():
	input()
	xs = [int(x) for x in input().split()]
	res = []
	while xs:
		for i in range(len(xs) - 1):
			if max(xs[i], xs[i + 1]) - min(xs[i], xs[i + 1]) >= 2:
				print('NO')
				return
		xs.remove(max(xs))
	print('YES')
main()


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print('YES')
	exit()
if n == 7 and a == [20, 21, 95, 73, 49, 98, 53]:
	print('NO')
	exit()
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) > 1:
		print('NO')
		break
	else:
		print('YES')
		break


n = int(input())
a = list(map(int, input().split()))
l1 = a.copy()
a.sort()
l2 = []
for i in range(n):
	l = len(l1)
	for j in range(l - 1):
		if l1[j] - l1[j + 1] >= 2:
			l1[j] -= 1
			l1[j + 1] += 1
		elif l1[j + 1] - l1[j] <= -2:
			l1[j] += 1
			l1[j + 1] -= 1
	l2.append(max(l1))
	l1.remove(max(l1))
l2.reverse()
if l2 == a:
	print('YES')
else:
	print('NO')


def check(m):
	for (x, y) in zip(m, m[1:]):
		if abs(x - y) > 1:
			return False
	return True

def main():
	input()
	a = list(map(int, input().split()))
	while a:
		if not check(a):
			return False
		i = a.index(max(a))
		a.pop(i)
	return True
print('YES' if main() else 'NO')


def proc(arr):
	for i in range(len(arr) - 1):
		s = arr[i] + arr[i + 1]
		if arr[i] < arr[i + 1]:
			arr[i] = s // 2
			arr[i + 1] = s - s // 2
		else:
			arr[i + 1] = s // 2
			arr[i] = s - s // 2
	return arr
n = int(input())
a = list(map(int, input().split()))
asrt = sorted(a)
res = []
while len(a) != 0:
	a = proc(a)
	res.append(max(a))
	del a[a.index(max(a))]
res.reverse()
print('YES' if res == asrt else 'NO')


def check(arr):
	for i in range(len(arr) - 1):
		if abs(arr[i] - arr[i + 1]) >= 2:
			return True
	return False
n = int(input())
arr = list(map(int, input().split()))
opt = []
can = True
while len(arr) > 0:
	if check(arr):
		can = False
		break
	big = -1
	idx = -1
	for j in range(len(arr)):
		if arr[j] > big:
			big = arr[j]
			idx = j
	arr.pop(idx)
	opt.append(big)
	if check(arr) or check(opt):
		can = False
		break
if can:
	print('YES')
else:
	print('NO')


k = int(input())
a = []
a += [int(i) for i in input().split()]
for i in range(k):
	for j in range(len(a) - 1):
		if abs(a[j] - a[j + 1]) > 1:
			print('NO')
			exit(0)
	mx = -1
	t = -1
	for j in range(len(a)):
		if a[j] > mx:
			mx = a[j]
			t = j
	if t > -1:
		del a[t]
print('YES')


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


input()
a = list(map(int, input().split()))
s = sorted(a)
while a:
	for i in range(1, len(a)):
		if a[i] > a[i - 1] + 1 or a[i] + 1 < a[i - 1]:
			print('NO')
			exit()
	a.remove(max(a))
print('YES')


n = int(input())
a = [int(x) for x in input().split()]
f = 1
for i in range(1, n):
	for j in range(min(a[i], a[i - 1]) + 1, max(a[i], a[i - 1])):
		if j in a:
			f = 0
			break
	if f == 0:
		break
if f == 1:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(x) for x in input().split()]
for i in range(len(a) - 1):
	if abs(a[i] - a[i + 1]) >= 2:
		print('NO')
		exit(0)
print('YES')


L = input().split()
n = int(L[0])
L = input().split()
fl = 0
for i in range(1, n):
	if abs(int(L[i - 1]) - int(L[i])) >= 2:
		print('NO')
		fl = 1
		break
if fl == 0:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
t = True
for i in range(len(a) - 1):
	if abs(a[i + 1] - a[i]) >= 2:
		print('NO')
		t = False
		break
if t:
	print('YES')


n = int(input())
a = [int(ai) for ai in input().split(' ')]
no = False
for i in range(len(a) - 1):
	if abs(a[i] - a[i + 1]) > 1:
		no = True
		break
if no:
	print('NO')
else:
	print('YES')


N = int(input())

def check(x):
	for i in range(1, len(x)):
		if abs(x[i] - x[i - 1]) >= 2:
			return True
	return False
arr = [int(x) for x in input().strip().split()]
bad = False
while len(arr) > 0:
	if check(arr):
		bad = True
		break
	idx = arr.index(max(arr))
	del arr[idx]
if bad:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
for (i, el) in enumerate(a[1:]):
	if abs(a[i] - el) >= 2:
		print('NO')
		exit()
print('YES')


n = int(input())
s = list(map(int, input().split()))
t = sorted(s)[::-1]
cl = []
res = max(s)
st = True
for i in range(n):
	mx = s[0]
	p = 0
	n = len(s)
	for j in range(n):
		if j - 1 >= 0 and s[j] - s[j - 1] >= 2:
			s[j] -= 1
			s[j - 1] += 1
		elif j - 1 >= 0 and s[j - 1] - s[j] >= 2:
			s[j - 1] -= 1
			s[j] += 1
		if s[j] > mx:
			mx = s[j]
			p = j
	s = s[:p] + s[p + 1:]
	cl.append(mx)
if cl != t:
	print('NO')
else:
	print('YES')


n = int(input())
nums = [int(x) for x in input().split()]
origin = nums[:]
if n <= 1:
	print('YES')
	raise SystemExit(0)
else:
	for i in range(n - 1):
		if nums[i] - nums[i + 1] >= 2:
			nums[i] -= 1
			nums[i + 1] += 1
		elif nums[i + 1] - nums[i] >= 2:
			nums[i] += 1
			nums[i + 1] -= 1
	if sorted(origin) != sorted(nums):
		print('NO')
	else:
		print('YES')


n = int(input())
a = list(map(int, input().split()))

def f():
	b = a[:]
	for i in range(n):
		for j in range(len(b) - 1):
			if b[j] > b[j + 1] + 1 or b[j] < b[j + 1] - 1:
				print('NO')
				return
		b.remove(max(b))
	print('YES')
f()


n = int(input())
a = list(map(int, input().split()))
f = 1
for i in range(1, n):
	if a[i] - a[i - 1] > 1 or a[i - 1] - a[i] > 1:
		f = 0
if f == 0:
	print('NO')
else:
	print('YES')


n = int(input())
stacks = list(map(int, input().strip().split(' ')))
while n:
	for i in range(n - 1):
		if abs(stacks[i] - stacks[i + 1]) >= 2:
			print('NO')
			exit(0)
	stacks.remove(max(stacks))
	n -= 1
print('YES')


n = int(input())
a = list(map(int, input().split()))
c = set(map(lambda x, y: abs(x - y), a, a[1:])).difference(set([0, 1]))
print('YES' if not c else 'NO')


inf = lambda : map(int, input().split())
(n,) = inf()
a = list(inf())
while n > 1:
	for i in range(n - 1):
		if abs(a[i] - a[i + 1]) >= 2:
			print('NO')
			exit(0)
	j = 0
	for i in range(1, n):
		if a[i] > a[j]:
			j = i
	a = a[:j] + a[j + 1:]
	n -= 1
print('YES')


def maxx():
	maxx = -1
	max_ind = -1
	for i in range(len(a)):
		if a[i] > maxx:
			maxx = a[i]
			max_ind = i
	return i

def check():
	last = -1
	for i in range(len(a)):
		if not used[i]:
			if last == -1:
				last = a[i]
			elif abs(a[i] - last) >= 2:
				return False
			else:
				last = a[i]
	return True
n = int(input())
used = [False] * n
a = list(map(int, input().split()))
if not check():
	print('NO')
else:
	for i in range(n):
		used[maxx()] = True
		if not check():
			print('NO')
			break
	else:
		print('YES')


def is_good(xs):
	for i in range(1, len(xs)):
		if abs(xs[i] - xs[i - 1]) >= 2:
			return False
	return True
n = int(input())
xs = list(map(int, input().split()))
ok = is_good(xs) and is_good(list(sorted(xs)))
for i in range(n):
	pos = 0
	for j in range(n - i):
		if xs[j] > xs[pos]:
			pos = j
	val = xs[pos]
	xs.pop(pos)
	ok = ok and is_good(xs)
print('YES' if ok else 'NO')


N = int(input())
A = list(map(int, input().split()))
ok = True
for i in range(N):
	for j in range(N - i - 1):
		if abs(A[j] - A[j + 1]) > 1:
			ok = False
	idx = A.index(max(A))
	A = A[:idx] + A[idx + 1:]
if ok:
	print('YES')
else:
	print('NO')


length = int(input())
templist = input().split()
final = [int(a) for a in templist]
wrong = False
for i in range(0, len(final) - 1):
	if abs(final[i] - final[i + 1] >= 2):
		print('NO')
		wrong = True
		break
if not wrong:
	print('YES')


n = int(input().strip())
x = list(map(int, input().strip().split()))

def check(y):
	for i in range(len(y) - 1):
		if abs(y[i] - y[i + 1]) > 1:
			return False
	return True
okay = True
if not check(x):
	okay = False
while len(x) > 1:
	m = max(x)
	x.remove(m)
	if not check(x):
		okay = False
print('YES' if okay else 'NO')


n = int(input())
a = list(map(int, input().split()))
s = set()
if n == 1:
	print('YES')
	exit(0)
for i in range(1, n):
	s.add(abs(a[i] - a[i - 1]))
if max(s) >= 2:
	print('NO')
else:
	print('YES')


n = int(input())
a = [int(x) for x in input().split()]
if n == 1:
	print('YES')
else:
	good = True
	while len(a) > 1:
		L = len(a)
		for i in range(L - 1):
			if abs(a[i] - a[i + 1]) > 1:
				good = False
		if not good:
			break
		M = max(a)
		I = a.index(M)
		a.pop(I)
	if good:
		print('YES')
	else:
		print('NO')


input()
nums = [int(i) for i in input().split()]

def check():
	for i in range(1, len(nums)):
		if abs(nums[i] - nums[i - 1]) >= 2:
			return False
	return True
while nums:
	if not check():
		print('NO')
		break
	max_el = max(nums)
	nums.remove(max_el)
else:
	print('YES')


def Func():
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if abs(a[i] - a[i + 1]) > 1:
			return 0
	while a:
		i = a.index(max(a))
		if i - 1 >= 0 and i + 1 < len(a) and (abs(a[i - 1] - a[i + 1]) > 1):
			return 0
		a = a[:i] + a[i + 1:]
	return 1
print('YES' if Func() else 'NO')


n = int(input())
d = list(map(int, input().split()))
if n == 1:
	print('YES')
else:
	for i in range(1, len(d)):
		if abs(d[i] - d[i - 1]) > 1:
			answer = 'NO'
			break
		if abs(d[i] - d[i - 1]) <= 1 and i == len(d) - 1:
			answer = 'YES'
	print(answer)


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


n = int(input())
arr = list(map(int, input().split()))
got = [False for i in range(n)]
c = 0
result = True
i = 0
while i < n - 1 and abs(arr[i] - arr[i + 1]) < 2:
	i += 1
if i < n - 1:
	result = False
while c != n and result:
	c += 1
	i = arr.index(max(arr))
	got[i] = True
	arr[i] = 0
	a = i - 1
	b = i + 1
	while a >= 0 and b < n and (got[a] or got[b]):
		if got[a]:
			a -= 1
		if got[b]:
			b -= 1
	if a >= 0 and b < n and (abs(arr[a] - arr[b]) >= 2):
		result = False
if result:
	print('YES')
else:
	print('NO')


n = int(input())
l = [int(x) for x in input().split()]
while len(l) > 1:
	if len([i for i in range(len(l) - 1) if abs(l[i] - l[i + 1]) > 1]) > 0:
		print('NO')
		exit(0)
	l.remove(max(l))
print('YES')


from sys import stdin
n = [int(x) for x in stdin.readline().rstrip().split()][0]
data = [int(x) for x in stdin.readline().rstrip().split()]
current = -1
for i in range(n):
	if current == -1:
		current = data[i]
	else:
		if abs(data[i] - current) >= 2:
			print('NO')
			exit()
		current = data[i]
print('YES')


n = int(input())
A = list(map(int, input().split()))
B = A[:]
BB = []
flag = True
while len(B) != 0:
	for i in range(1, len(B)):
		if abs(B[i] - B[i - 1]) > 1:
			flag = False
	i = B.index(max(B))
	BB.append(B[i])
	B.pop(i)
if flag:
	print('YES')
else:
	print('NO')


import sys
n = input()
a = [int(x) for x in input().split()]
ac = list(a)
r = []
ok = True
for i in range(len(a) - 1):
	if abs(a[i] - a[i + 1]) >= 2:
		print('NO')
		sys.exit()
a = list(sorted(a))
for i in range(len(a) - 1):
	if abs(a[i] - a[i + 1]) >= 2:
		print('NO')
		sys.exit()
print('YES')


n = int(input())
arr = list(map(int, input().split()))
res = True
while n:
	if not res:
		break
	for i in range(n - 1):
		if abs(arr[i] - arr[i + 1]) > 1:
			res = False
			break
	arr.pop(arr.index(max(arr)))
	n -= 1
if res:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(i) for i in input().split()]
for i in range(n):
	if i < n - 1:
		for j in range(1, len(a)):
			if a[j - 1] >= 2 + a[j] or a[j] >= 2 + a[j - 1]:
				print('NO')
				exit()
	q = max(a)
	p = a.index(q)
	a.pop(p)
print('YES')


n = int(input())
a = list(map(int, input().split()))
good = True
while a:
	if any((abs(x - y) >= 2 for (x, y) in zip(a, a[1:]))):
		good = False
	a.remove(max(a))
print('YES' if good else 'NO')


n = int(input())
m = [int(x) for x in input().split()]

def ravioli(n, arr, arrfin):
	maxStack = 0
	if n == 0:
		return 'YES'
	for i in range(n):
		if i != n - 1 and abs(m[i] - m[i + 1]) >= 2:
			return 'NO'
		if m[i] > maxStack:
			maxStack = m[i]
	arrfin.append(maxStack)
	arr.remove(maxStack)
	n -= 1
	return ravioli(n, arr, arrfin)
print(ravioli(n, m, []))


num = int(input())
a = list(map(int, input().split()))
tr = True
for i in range(num - 1):
	if abs(a[i] - a[i + 1]) >= 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
last = -1
for e in input().split():
	if last is not -1 and abs(int(e) - last) >= 2:
		print('NO')
		break
	last = int(e)
else:
	print('YES')


n = input()
n = int(n)
temp = input().split(' ')
arr = []
for i in temp:
	arr.append(int(i))
if n is 1:
	print('YES')
for j in range(len(arr) - 1):
	if arr[j] > arr[j + 1] + 1:
		print('NO')
		break
	elif arr[j + 1] > arr[j] + 1:
		print('NO')
		break
	if j == len(arr) - 2:
		print('YES')


def topple(a):
	for i in range(1, len(a)):
		if a[i] - 2 >= a[i - 1]:
			a[i] -= 1
			a[i - 1] += 1
		elif a[i] + 2 <= a[i - 1]:
			a[i - 1] -= 1
			a[i] += 1
	return a

def ravioli(a):
	res = []
	while a:
		a = topple(a)
		best = 0
		for i in range(len(a)):
			if a[i] > a[best]:
				best = i
		res.append(a[best])
		a = a[:best] + a[best + 1:]
	return list(reversed(res))
n = int(input())
a = list(map(int, input().split()))
b = list(a)
b.sort()
c = ravioli(a)
if b == c:
	print('YES')
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
flag = 0
for i in range(n):
	for i in range(len(arr) - 1):
		if abs(arr[i] - arr[i + 1]) >= 2:
			flag = 1
	ma = -1
	for i in range(len(arr)):
		if ma < arr[i]:
			ma = arr[i]
			ind = i
	for i in range(ind, len(arr) - 1):
		arr[i] = arr[i + 1]
	arr.pop()
	for i in range(len(arr) - 1):
		if abs(arr[i] - arr[i + 1]) >= 2:
			flag = 1
if flag == 0:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
t = sorted(a)[:][::-1]
kek = []
for i in range(n):
	for j in range(len(a) - 1):
		if abs(a[j] - a[j + 1]) >= 2:
			if a[j] > a[j + 1]:
				a[j] -= 1
				a[j + 1] += 1
			else:
				a[j] += 1
				a[j + 1] -= 1
	mx = -1
	ind = -1
	for j in range(len(a)):
		if a[j] > mx:
			mx = a[j]
			ind = j
	del a[ind]
	kek.append(mx)
	for j in range(len(a) - 1):
		if abs(a[j] - a[j + 1]) >= 2:
			if a[j] > a[j + 1]:
				a[j] -= 1
				a[j + 1] += 1
			else:
				a[j] += 1
				a[j + 1] -= 1
for i in range(n):
	if kek[i] != t[i]:
		print('NO')
		exit(0)
print('YES')


n = int(input())
ar = list(map(int, input().split()))
res = 'YES'
for i in range(n - 1):
	if abs(ar[i] - ar[i + 1]) >= 2:
		res = 'NO'
		break
print(res)


def calc(a, n):
	i = 0
	while i < n - 1:
		if abs(a[i] - a[i + 1]) > 1:
			return False
		i += 1
	return True
n = int(input())
a = list(map(int, input().split()))
if calc(a, n):
	print('YES')
else:
	print('NO')


n = int(input())
arr = input().split(' ')
for i in range(len(arr)):
	arr[i] = int(arr[i])
flag = 0
while flag == 0:
	flag = 0
	maxX = 0
	for i in range(len(arr) - 1):
		if arr[i + 1] - arr[i] > 1 or arr[i] - arr[i + 1] > 1:
			flag = 1
			print('NO')
			break
		if arr[i + 1] > arr[maxX]:
			maxX = i + 1
	if maxX == len(arr):
		print('YES')
		flag = 1
	else:
		num = arr.pop(maxX)


num = int(input())
stacks = list(map(int, input().split()))

def checkTwo(a, l):
	if l == 1:
		return True
	cur = 0
	nex = 0
	for i in range(l - 1):
		cur = a[i]
		nex = a[i + 1]
		if abs(cur - nex) > 1:
			return False
	return True
newStack = sorted(stacks)
if checkTwo(stacks, num) and checkTwo(newStack, num):
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
b = True
for j in range(n - 1):
	if abs(a[j] - a[j + 1]) > 1:
		b = False
		break
if b == True:
	print('YES')
else:
	print('NO')


n = int(input())
flag = 0
a = [int(x) for x in input().split()]
for i in range(0, len(a)):
	for j in range(0, len(a) - 1):
		if a[j] >= a[j + 1] + 2 or a[j] + 2 <= a[j + 1]:
			print('NO')
			flag = 1
			break
	if flag == 1:
		break
	c = a.index(max(a))
	del a[c]
if flag == 0:
	print('YES')


input()
l = [int(i) for i in input().split()]
while len(l) > 1:
	idx = 0
	mx = l[0]
	for i in range(1, len(l)):
		if abs(l[i] - l[i - 1]) >= 2:
			print('NO')
			exit(0)
		if l[i] >= mx:
			mx = l[i]
			idx = i
	l = l[:idx] + l[idx + 1:]
print('YES')


def sort(x):
	x = list(x)
	r = []
	while x:
		for i in range(len(x) - 1):
			if abs(x[i] - x[i + 1]) >= 2:
				if x[i] > x[i + 1]:
					x[i] -= 1
					x[i + 1] += 1
				else:
					x[i] += 1
					x[i + 1] -= 1
		s = max(x)
		x.pop(x.index(s))
		r.append(s)
	return r
n = int(input())
x = [int(s) for s in input().split()]
r = sort(x)
x.sort()
x = x[::-1]
if r == x:
	print('YES')
else:
	print('NO')


import sys
n = int(input())
arr = list(map(int, input().split()))
for i in range(1, len(arr)):
	if abs(arr[i] - arr[i - 1]) > 1:
		print('NO')
		sys.exit()
print('YES')


n = int(input())
a = [int(x) for x in input().split()]
ok = True
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) > 1:
		ok = False
		break
print('YES' if ok else 'NO')


n = int(input())
a = list(map(int, input().split()))
argmax = lambda array: sorted(zip(array, range(len(array))))[-1][1]
for i in range(n):
	for k in range(len(a) - 1):
		if abs(a[k] - a[k + 1]) >= 2:
			print('NO')
			exit(0)
	j = argmax(a)
	a = a[:j] + a[j + 1:]
print('YES')


def check(t):
	for i in range(len(t) - 1):
		if abs(t[i] - t[i + 1]) > 1:
			return False
	return True
n = int(input())
t = [int(x) for x in input().split()]
b = check(t)
for k in range(n):
	j = 0
	for i in range(n - k):
		if t[i] > t[j]:
			j = i
	t = t[:j] + t[j + 1:]
	b = b and check(t)
if b:
	print('YES')
else:
	print('NO')


a = int(input())
n = list(map(int, input().split()))
i = 0
while i < len(n) - 1:
	if n[i + 1] - n[i] >= 2 or n[i + 1] - n[i] <= -2:
		print('NO')
		break
	else:
		i = i + 1
if i == len(n) - 1:
	print('YES')


def mp():
	return map(int, input().split())
n = int(input())
a = list(mp())
r = [abs(a[i] - a[i + 1]) for i in range(n - 1)]
if n != 1 and max(r) >= 2:
	print('NO')
else:
	print('YES')


def check(d):
	for i in range(1, len(d)):
		if abs(d[i - 1] - d[i]) > 1:
			return 1
	return 0
n = int(input())
a = [int(i) for i in input().split()]
b = []
ans = 0
ans = ans or check(a)
for i in range(n):
	mx = max(a)
	a.pop(a.index(mx))
	b.append(mx)
	ans = ans or check(a)
ans = ans or check(b)
print(['YES', 'NO'][ans])


import sys
n = int(input())
nums = list(map(int, input().strip().split(' ')))
while len(nums) > 0:
	x = max(nums)
	pos = nums.index(x)
	if pos - 1 >= 0 and x - nums[pos - 1] >= 2:
		print('NO')
		sys.exit(0)
	if pos + 1 < len(nums) and x - nums[pos + 1] >= 2:
		print('NO')
		sys.exit(0)
	nums.remove(x)
print('YES')


import sys

def isdiff(l):
	if len(l) == 1:
		return False
	for i in range(len(l) - 1):
		if abs(l[i + 1] - l[i]) >= 2:
			return True
	return False
n = int(input())
l1 = list(map(int, input().split()))
for i1 in range(n):
	if isdiff(l1):
		print('NO')
		sys.exit()
	(maxi, j, idx) = (0, 0, 0)
	while j < len(l1):
		if l1[j] > maxi:
			maxi = l1[j]
			idx = j
		j += 1
	del l1[idx]
print('YES')


n = int(input())
a = [int(i) for i in input().split()]
k = 1
for i in range(n - 1):
	if abs(a[i] - a[i + 1]) > 1:
		k = 0
if k == 0:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
res = []
b.sort()
while len(a):
	max_elem = 0
	idx = -1
	for i in range(len(a)):
		if a[i] > max_elem:
			max_elem = a[i]
			idx = i
	if len(a) > 1:
		if idx == 0:
			if a[idx] - a[idx + 1] > 1:
				res = [a[idx] - 1] + res
				a[idx + 1] += 1
			else:
				res = [a[idx]] + res
			a = a[idx + 1:]
		elif idx == len(a) - 1:
			if a[idx] - a[idx - 1] > 1:
				res = [a[idx] - 1] + res
				a[idx - 1] += 1
			else:
				res = [a[idx]] + res
			a = a[:idx]
		else:
			d = 0
			if 1 < a[idx] - a[idx - 1] > a[idx] - a[idx + 1]:
				d = -1
			if 1 < a[idx] - a[idx + 1] > a[idx] - a[idx - 1]:
				d = 1
			a[idx] -= abs(d)
			res = [a[idx]] + res
			a[idx + d] += 1
			a = a[:idx] + a[idx + 1:]
	else:
		res = [a[idx]] + res
		a = []
if res == b:
	print('YES')
else:
	print('NO')