Problem:
 Score : 200 points    Problem Statement We have a sequence p = { p_1,\ p_2,\ ...,\ p_N } which is a permutation of { 1,\ 2,\ ...,\ N }.  You can perform the following operation at most once: choose integers i and j  (1 \leq i < j \leq N) , and swap p_i and p_j . Note that you can also choose not to perform it.  Print YES if you can sort p in ascending order in this way, and NO otherwise.      Constraints  All values in input are integers.  2 \leq N \leq 50  p is a permutation of { 1,\ 2,\ ...,\ N }.         Input Input is given from Standard Input in the following format:  N  p_1  p_2  ...  p_N       Output Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.        Sample Input 1 5
5 2 3 4 1      Sample Output 1 YES  You can sort p in ascending order by swapping p_1 and p_5 .       Sample Input 2 5
2 4 3 5 1      Sample Output 2 NO  In this case, swapping any two elements does not sort p in ascending order.       Sample Input 3 7
1 2 3 4 5 6 7      Sample Output 3 YES  p is already sorted in ascending order, so no operation is needed.

Solution (Python):
import sys, math

def main():
    N = int(input())
    P = tuple(map(int,input().split()))

    sP = sorted(P)

    counter = 0
    for x,y in zip(P, sP):
        if x != y:
            counter += 1

    if counter <= 2:
        print('YES')
    else:
        print('NO')

if __name__=='__main__':
    main()

Solution (Python):
size = int(input())

numbers = [int(s) for s in input().split()]

unexpected_times = 0

for i in range(size):
    if i + 1 != numbers[i]:
        unexpected_times += 1


if unexpected_times == 0 or unexpected_times == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
S = sorted(P)

num = 0
for p, s in zip(P, S):
    if p != s:
        num += 1

if num <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
p=input().rstrip().split(' ')
C=list(p)
B=0;
C.sort(key=int)
for i in range(0,len(p)):
    if int(p[i])!=int(C[i]):
        B+=1;
if B<=2:
    print("YES")
else:
    print("NO")

Solution (Python):
N=int(input())
p=list(map(int, input().split()))
q=sorted(p)
t=0
for i in range(N):
    if p[i]!=q[i]:
        t+=1
if t==2 or t==0:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
pl = list(map(int, input().split()))

pl_sorted = sorted(pl)
n_diff = 0
for i in range(N):
    if pl[i] != pl_sorted[i]:
        n_diff += 1

print("YES") if n_diff <= 2 else print("NO")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
a = sorted([(p[i], i + 1) for i in range(n)])
count = 0
for i in a:
    if i[0] != i[1]:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
def INT_(n): return int(n)-1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_, input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split())) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')


def main():
    N = I()
    ps = LI()
    cnt = 0
    for i, p in enumerate(ps):
        if i+1 != p:
            cnt += 1

    if cnt <= 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

p2 = sorted(p)
cnt = 0

for i in range(N) :
    if p[i] != p2[i] :
        cnt += 1

if cnt > 2 :
    print('NO')
else :
    print('YES')

Solution (Python):
import itertools
import sys

n = int(input())
p_list = list(map(int, input().split()))
is_sorted = all(p_list[k] < p_list[k+1] for k in range(n - 1))
if is_sorted:
    print('YES')
    sys.exit()
for i, j in itertools.combinations(range(n), 2):
    p_list[i], p_list[j] = p_list[j], p_list[i]
    is_sorted = all(p_list[k] < p_list[k+1] for k in range(n - 1))
    if is_sorted:
        print('YES')
        sys.exit()
    p_list[i], p_list[j] = p_list[j], p_list[i]
print('NO')

Solution (Python):

N = int(input())
A = list(map(int,input().split()))

counta = 0
for i in range(N):
    if A[i] == i + 1:
        continue
    else:
        counta += 1

if counta == 2 or counta == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys


def main():
    n_list = [int(s) for s in sys.stdin.readline().strip().split()]
    n = n_list[0]
    p = [int(s) for s in sys.stdin.readline().strip().split()]
    sorted_p = sorted(p)
    diff = 0
    for i in range(n):
        if p[i] != sorted_p[i]:
            diff += 1
    if diff == 0 or diff == 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
p = tuple(map(int,input().split()))
c = 0
ans = "YES"
for i in range(n) :
    if p[i] != i+1 :
        c += 1
if c > 2 :
    ans = "NO"
print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
s = sorted(l)
cnt = 0
for i in range(n):
    if l[i] != s[i]: cnt += 1
print("YES" if cnt <= 2 else "NO")

Solution (Python):
import math

N = input()
sample = list(map(int, input().split()))
sorted_sample = sorted(sample)
count = 0

for x, y in zip(sample, sorted_sample):
  if x != y:
    count += 1
  if count >2:
    print("NO")
    break

if count <=2:
  print("YES")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

q = sorted(p)

diff = 0
for n in range(N):
    if p[n]!=q[n]:
        diff += 1

if diff<=2:
    print('YES')
else:
    print('NO')

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
p1=sorted(p)
count=0
for i in range(len(p)):
  if p[i]!=p1[i]:
    count+=1
if count==2 or count==0:
  print("YES")
else:
  print("NO")

Solution (Python):
N = int(input())
p_list = list(map(int,input().split()))
p_list_sorted = sorted(p_list)
cnt = 0
for i in range(N):
    if p_list[i] != p_list_sorted[i]:
        cnt += 1

if cnt == 2 or cnt == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
import copy
n = int(input())
p = input().split()
for i in range(len(p)):
    p[i] = int(p[i])
l = copy.deepcopy(p)
l.sort()
k = 0
for i in range(len(l)):
    if l[i]!=p[i]:
        k+=1
if k==2or k==0:
    print("YES")
else:
    print("NO")

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))

    for i in range(n):
        for j in range(i + 1, n):
            b = [k for k in a]
            b[i], b[j] = b[j], b[i]
            if b == [k for k in range(1, n + 1)]:
                print("YES")
                return

    print("YES" if a == [k for k in range(1, n + 1)] else "NO")

main()

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

Q = list(range(1, N+1))

if sum([p!=q for p, q in zip(P, Q)]) <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
L = [int(x) for x in input().split()]

count = 0
for i in range(N):
	if((i+1)!=L[i]):
		count +=1

if(count < 3):
	print("YES")
else:
	print("NO")

Solution (Python):
N = int(input())
p_list = list(map(int, input().split(" ")))

p_list_true = sorted(p_list)

flag = True
diff_indexs = []
for i in range(N):
    if p_list[i] != p_list_true[i]:
        diff_indexs.append(i)
if len(diff_indexs) == 0:
    print("YES")
elif len(diff_indexs) == 2:
    i, j = diff_indexs
    if p_list[i] == p_list_true[j] and p_list[j] == p_list_true[i]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Solution (Python):
n = int(input())

p = list(map(int, input().split()))

ans = [x + 1 for x in range(n)]
ans = sorted(ans)

count = 0

for i in range(n):
    if ans[i] != p[i]:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

counter = 0

buff = P.copy()

buff.sort()

for i in range(N):
    if P[i] != buff[i]:
        counter += 1

if counter <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n=int(input())
p=list(map(int, input().split()))
s=[i+1 for i in range(n)]
count=0

for i in range(n):
    if p[i]!=s[i]:
        count+=1
if count<=2:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy




def main():
    N = int(input())
    pp = list(map(int, input().split()))

    c = 0
    for p1,p2 in zip(pp, sorted(pp)):
        if p1 != p2:
            c += 1

    if c == 2 or c == 0:
        print("YES")
    else:
        print("NO")

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

p2 = sorted(p)

cnt = 0
for i in range(n):
    if p[i] != p2[i]:
        cnt += 1

if cnt in [0,2]:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

count = 0 
a = []

for i in range(n):
    if not p[i] == i + 1:
        a.append(i)
        count += 1

if len(a)==0:
    print("YES")
elif len(a)==2:
    if p[a[0]]==a[1]+1 and p[a[1]]==a[0]+1:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
sp = sorted(p)
d = 0
for i in range(n):
    if p[i] != sp[i]:
        d += 1
    if d > 2:
        break
if d > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(N):
    if p[i] != i+1:
        cnt += 1
if cnt <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):


import math
import sys
import itertools
import numpy as np


n =int(input())


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

p = sorted(k)
diff=0
for i in range(n):
    if k[i] != p[i]:
        diff += 1

if diff<3:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
Pn = list(map(int, input().split()))

Qn = list(range(1, N+1))

cnt = 0
for i in range(N):
    if Pn[i] != Qn[i]:
        cnt += 1
    else:
        continue

if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p_list = list(map(int, input().split()))

default_list = list(range(1, n + 1))

diff = 0
for i in range(n):
    if p_list[i] != default_list[i]:
        diff += 1

if diff == 0 or diff == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
p = list(map(int, input().split()))
for i in range(n):
    if i+1 != p[i]:
        cnt += 1
if cnt <= 2:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

Q = sorted(P)

count = N
for i in range(N):
    if P[i] == Q[i]:
        count -= 1
if count == 0 or count == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
ps = [int(pi) for pi in input().split(' ')]
trues = list(range(1, N+1))

bools = [pi != ti for pi, ti in zip(ps, trues)]
S = sum(bools)
if S == 0 or S == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]

cnt = 0
for i in range(n):
    if i+1 != p[i]:
        cnt += 1

if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]

sp = sorted(p)
count = 0
for i in range(n):
    if p[i] != sp[i]:
        count += 1

if count == 0 or count == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
def main():
    n = int(input())
    p = list(map(int, input().split()))
    q = sorted(p)
    count = 0
    for i in range(n):
        if p[i] != q[i]:
            count += 1
    if count == 0 or count == 2:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]
T = [ i for i in range(1,N+1)]

missc = 0
for i in range(N):
    if P[i]!=T[i]:
        missc+=1

print( 'YES' if missc==2 or missc==0 else 'NO')

Solution (Python):
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N = int_raw()
    As = ints_raw()
    Ass = sorted(As)
    nc = sum([As[i]!=Ass[i] for i in range(N)])
    if nc <=2:
        return "YES"
    else:
        return "NO"

print(main())

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

Q = sorted(P)
num = 0
for i in range(N):
    if P[i] != Q[i]:
        num += 1
if num <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p = [i for i in map(int,input().split())]
p_sorted = sorted(p)
count = 0
for i in range(len(p)):
    if p[i] != p_sorted[i]:
        count +=1
if (count == 0) or (count ==2):
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p_list = list(map(int, input().split()))

ap_list = sorted(p_list, reverse = False)

counter = 0

for i in range(N):
    if p_list[i] != ap_list[i]:
        counter += 1

if counter <= 2:
    print("YES")

else:
    print("NO")

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]

sorted_p = sorted(p)

count = 0
for i in range(n):
    if p[i] != sorted_p[i]:
        count += 1

if count > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]
P_sort = sorted(P)
import numpy as np
P_np = np.array(P)
P_sort_np = np.array(P_sort)
if len(P) == (P_np == P_sort_np).sum() or len(P)-2 == (P_np == P_sort_np).sum():
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]
p_sorted = list(sorted(P))

if P == p_sorted:
    print('YES')
    exit()
for i in range(N):
    for j in range(i + 1, N):
        P[i], P[j] = P[j], P[i]
        if P == p_sorted:
            print('YES')
            exit()
        P[i], P[j] = P[j], P[i]
print('NO')

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
c = 0
for i in range(n):
    c += (p[i]!=i+1)
print('YES' if c<=2 else 'NO')

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

right = list(range(1,n+1))
count = 0
for i in range(n):
  if p[i]!=right[i]:
    count += 1

if count==0 or count==2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
x = 0
for i in range(n):
    if p[i] != i + 1:
        x += 1
if x == 0 or x == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
P = [int(zz) for zz in input().split()]

flag = 0
for i in range(N):
    if i+1 != P[i]:
        flag += 1

if flag == 2 or flag == 0:
    print('YES')
else:
    print('NO')

Solution (Python):

N = int(input())
P = list(map(int, input().split()))
gold = [i for i in range(1, N+1)]
err_cnt = 0
for i, j in zip(P, gold):
    if i != j:
        err_cnt += 1
if err_cnt <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
c=0
a=[]
b=[]
for i in range(n):
    if p[i]!=i+1:
        c+=1
        a.append(i+1)
        b.append(p[i])
if c==0:
    print("YES")
elif c==2:
    if a[0]==b[1]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))

count = 0
for i, item in enumerate(arr):
  if item != i+1:
    count += 1
if count == 2 or count == 0:
  print("YES")
else:
  print("NO")

Solution (Python):

n = int(input())
data = list(map(int, input().split()))
x = list(range(1, n + 1))
cnt = 0
for i in range(n):
    if data[i] != x[i]:
        cnt += 1

if cnt <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
a_s = [int(item) for item in input().split()]

a_sorted = sorted(a_s)

cnt = 0
for std, a in zip(a_sorted, a_s):
    if std != a:
        cnt += 1

if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
Q = sorted(P)
s = 0
for i in range(n):
    if P[i] != Q[i]:
        s += 1
if s <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict






INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)








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

B = sorted(A)

cnt = 0
for i in range(N):
    if A[i] != B[i]:
        cnt += 1

if cnt <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]

count = 0
sorted_p = sorted(p)
for i in range(len(p)):
  if sorted_p[i] != p[i]:
    count += 1

if count > 2:
  print('NO')
else: 
  print('YES')

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
c = 0
for i in range(N):
    if i+1 != P[i]:
        c += 1
if c > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

s = 0
for i in range(N):
    if i + 1 != p[i]:
        s += 1
if s > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]

cnt = 0
for i in range(N):
    if p[i] != i+1:
        cnt += 1
        

if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
a = list(range(1,n+1))
dif = 0
for i in range(n):
    if p[i] != a[i]:
        dif += 1
if dif == 0 or dif == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    N = II()
    P= LI()

    P_sort = sorted(P)

    diff = 0
    for i,j in zip(P, P_sort):
        if i != j:
            diff += 1
    if diff ==0 or diff ==2:
        print('YES')
    else:
        print('NO')

main()

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
rev = 0
for i in range(n):
    if p[i] != i + 1:
        rev += 1
if rev <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
Ps = list(map(int, input().split()))
Ps.insert(0, 0)

c = 0
for i in range(1, N + 1):
    if Ps[i] != i:
        c += 1
if c == 2 or c == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
N=int(input())
p=list(map(int, input().split()))
l=sorted(p)

c=0
for i in range(N):
  if p[i]!=l[i]:
    c+=1

if c==0 or c==2:
  print('YES')
else:
  print('NO')

Solution (Python):
import sys, math
from collections import deque

readline = sys.stdin.readline

N = int(readline())
P = list(map(int, readline().split()))

a = len([i for i, p in enumerate(P) if i+1 != p])
if a <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
s = sorted(l)
count = 0
for i in range(n):
    if l[i] != s[i]:
        count += 1
if count == 2 or count == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
def main():
  n = int(input())
  plist = list(map(int, input().split()))
  sc = 0
  for i, p in enumerate(plist):
    if i+1 != p:
      sc += 1
  if sc == 2 or sc == 0:
    print('YES')
  else:
    print('NO')


if __name__ == '__main__':
  main()

Solution (Python):
import sys

n= int(sys.stdin.readline())
p= [ int(x) for x in sys.stdin.readline().split() ]
q= sorted(p)

c= 0
for i in range(n):
    if p[i]!=q[i]: c+=1
    if c>2: break

if c<=2: print( 'YES' )
else: print('NO' )

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
count = 0
for i in range(N):
    if P[i] != i+1:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)

def main():
	n = int(input())
	a = list(map(int,input().split()))
	b = deepcopy(a)
	b.sort()
	c = 0
	for i in range(n):
		if(b[i] != a[i]):
			c+=1
	if(c==2 or c==0):
		print('YES')
	else:
		print('NO')

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Solution (Python):

N = int(input())
p_input_lst = list(map(int, input().split()))

diff_count = 0
for i in range(N):
    if i+1 != p_input_lst[i]:
        diff_count += 1

if diff_count == 2 or diff_count == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
flag=False
for i in range(1,n):
  if arr[i]<arr[i-1]:
    break
else:
  flag=True
for i in range(n):
  for j in range(i+1,n):
    tmp=arr[:]
    tmp[i],tmp[j]=tmp[j],tmp[i]
    for k in range(1,n):
      if tmp[k]<tmp[k-1]:
        break
    else:
      flag=True
if flag==True:
  print('YES')
else:
  print('NO')

Solution (Python):
N=int(input())
P=list(map(int,input().split()))

n=[i+1 for i in range(N)]

cnt=0
for l,p in zip(n,P):
    if l != p:
        cnt+=1
if cnt <=2:
    print('YES')
else:
    print('NO')

Solution (Python):
N=input()
P=map(int, raw_input().split())
Q=sorted(P)

cnt=0
for p,x in zip(P,Q):
	if p!=x:
		cnt+=1

if cnt<=2:
	print "YES"
else:
	print "NO"

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
cnt = 0
for i,j in zip(data,range(1,N+1)):
    if i != j:
        cnt += 1
if cnt == 2 or cnt == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]

c = 0
for i in range(1, n+1):
    if p[i-1] != i:
        c += 1

if c >= 3:
    print("NO")
else:
    print("YES")

Solution (Python):
int(input())
ps = list(map(int, input().split()))

count = 0
for i, p in enumerate(ps):
    if i + 1 != p:
        count += 1

if count > 2:
    print('NO')
else:
    print('YES')

Solution (Python):
n = int(input())
l = list(map(int,input().split()))

def upright(ll):
    for i in range(len(ll)-1):
        if ll[i]>ll[i+1]:
            return False
    return True

if upright(l):
    print("YES")
    exit(0)

for i in range(n):
    for j in range(i+1,n):
        tmp = l[i]
        l[i] = l[j]
        l[j] = tmp
        if upright(l):
            print("YES")
            exit(0)
        tmp = l[i]
        l[i] = l[j]
        l[j] = tmp
print("NO")

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

P_ = P.copy()
P.sort()
r = 0
for p, p_ in zip(P, P_):
  if p != p_:
    r += 1
  if r > 2:
    break
else:
  print("YES")
  exit()
print("NO")

Solution (Python):
def main():
    _ = int(input())
    *p, = map(int, input().split())

    res = [q for i, q in enumerate(p) if i + 1 != q]
    if len(res) in [0, 2]:
        print('YES')
    else:
        print('NO')


main()

Solution (Python):
N = int(input())

p = list(map(int,input().split()))


tmp_ans=0
for i in range(N):
    if(p[i]==i+1):
        tmp_ans+=1

if(tmp_ans==N or tmp_ans==N-2):
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
sp = sorted(p)
g = 0
for i in range(N):
    if p[i] != sp[i]:g += 1
if g <= 2:print('YES')
else:print('NO')

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
q = sorted(p)

c = 0
for i in range(N):
    if p[i] != q[i]:
        c += 1

if c == 2 or c == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

q = sorted(p)
d = 0
for i in range(N):
  if p[i] != q[i]:
    d += 1
if d == 0 or d == 2:
  print("YES")
else:
  print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(N):
    if p[i] != i+1:
        cnt += 1

if cnt > 2:
    print('NO')
else:
    print('YES')

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
ed = list(p)
ed.sort()

z=0
for a,b in zip(p,ed):
    if a!=b:
        z+=1
print("YES" if z <= 2 else "NO")

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): 
	return list(map(int,input().split()))
def m(): 
	return map(int,input().split())
def onem(): 
	return int(input())
def s(x): 
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): 
	return " ".join(map(str,x))
def max2(x): 
	return max(map(max,x))

n = onem()

p = l()

pp = p[:]
pp.sort()
if p == pp:
	print("YES")
	exit()

for i in range(n):
	for j in range(i+1,n):
		ppp = p[:]
		ppp[i],ppp[j] = ppp[j],ppp[i]
		if all(pp[k] == ppp[k] for k in range(n)):
			print("YES")
			exit()
else:
	print("NO")

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
    if not p[i] == i+1:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
q = sorted(p)
z = 0

for i,j in zip(p,q):
  if i!=j:
    z+=1

if z<=2:
  print("YES")
else:
  print("NO")

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]

c = 0
for i in range(N) :
    if p[i] != i + 1 :
        c += 1
        
if c <= 2 :
    print('YES')
else :
    print('NO')

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    ps = LI()
    ps_s = sorted(ps)
    c = 0
    for i in range(n):
        if ps[i] != ps_s[i]:
            c += 1
    if c < 3:
        print("YES")
    else:
        print("NO")

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]

cnt = 0
for k in range(len(p)):
    if p[k] != k + 1:
        cnt += 1

if cnt == 2 or cnt == 0:
    print('YES')
else:
    print('NO')

Solution (Python):

n = int(input())
p = list(map(int, input().split()))
ans = all(p[i] < p[i + 1] for i in range(n - 1))
for i in range(n):
    for j in range(i + 1, n):
        a = p[:]
        a[i], a[j] = a[j], a[i]
        f = True
        for k in range(n - 1):
            if a[k] >= a[k + 1]:
                f = False
        if f:
            ans = True
print(['NO', 'YES'][ans])

Solution (Python):
N=int(input())
Pn=[int(x) for x in input().split()]
sortedPn=sorted(Pn)
c=0
for i,Pi in enumerate(Pn):
  if sortedPn[i]!=Pi:
    c+=1

if c<=2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
x = []
for i in range(n):
  if p[i] != (i+1):
    x.append(p[i])

if len(x) == 0 or len(x)==2:
  print('YES')
else:
  print('NO')

Solution (Python):
N = int(input())
P = list(map(int, input().split()))

Ps = sorted(P)

cnt = 0
for i in range(N):
  if P[i] != Ps[i]:
    cnt += 1
    

print('YES' if cnt <= 2 else 'NO')

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

AA = sorted(A)

x = A[0]
cnt = 0
for i in range(N):
    if A[i] != AA[i]:
        cnt += 1

if cnt <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
def InputToInt():
    A = input().split()
    for i in range(len(A)):
        A[i] = int(A[i])
    return A
    

N = int(input())
p = InputToInt()

c = 0
for i in range(N):
    if p[i] != i+1:
        c += 1

if c <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p = [int(x) for x in input().split()]
numofchange = 0
for i in range(N):
    if p[i] != i+1:
        numofchange += 1
if numofchange <= 2:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = [i for i in range(1,n+1)]
if a == b:
    print("YES")
else:
    count = 0
    for i in range(n):
        if a[i] != b[i]:
            count += 1
    if count == 2:
        print("YES")
    else:
        print("NO")

Solution (Python):
n=int(input())
p=[int(i) for i in input().split()]
ans = p[:]
ans.sort()
for i in range(n):
  for j in range(n):
    p[i],p[j]=p[j],p[i]
    if p==ans:
      print("YES")
      exit()
    p[i],p[j]=p[j],p[i]
print("NO")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
p_sort = sorted(p)

cnt = 0
for i in range(n):
    if p[i] != p_sort[i]:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p = list(map(int,input().split()))

p_s = sorted(p)
count = 0
for i in range(N):
  if p[i] != p_s[i]:
    count += 1
if count <= 2:
  print('YES')
else:
  print('NO')

Solution (Python):
n=int(input())
hoge=list(map(int,input().split()))
ans=0
for i in range(n):
  if(hoge[i]!=i+1):
    ans+=1
    
if(ans==0 or ans==2):
  print("YES")
else:
  print("NO")