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):
N = [int(i) for i in input().split()][0]
P = [int(i) for i in input().split()]

flag=0
save=0
for i in range(N-1):
  if flag==0 and P[i]>P[i+1]:
    save=i
    flag=1
    continue
  if flag==1 and P[i]>P[i+1]:
    tmp=P[i+1]
    P[i+1]=P[save]
    P[save]=tmp
    break

flag=0
for i in range(N-1):
  if P[i]>P[i+1]:
    print('NO')
    flag=1
    break

if flag!=1:
  print('YES')

Solution (Python):
import copy
n = int(input())
a = list(map(int, input().split()))
a_sorted = sorted(a)
cant = True
if a == a_sorted:
	print("YES")
	quit()
for i in range(n):
	for j in range(n):
		a_c = copy.copy(a)
		if i >= j:
			continue
		a_c[i],a_c[j] = a_c[j],a_c[i]
		if a_c == a_sorted:
			cant = False
if cant:
	print("NO")
else:
	print("YES")

Solution (Python):



import sys
from copy import copy

input = sys.stdin.readline

def main():
    N = int(input())
    P = list(map(int, input().split()))
    P2 = copy(P)
    P2.sort()

    cnt = 0
    for i in range(N):
        if P[i] != P2[i]:
            cnt += 1
            if cnt == 3:
                print("NO")
                return
    print("YES")

main()

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

err=[]
err_idx=[]

for i in range(N):
  if (i+1)!=p[i]:
    err.append(p[i])
    err_idx.append(i)
if len(err)>=3:
  print("NO")
  exit()
elif len(err)==0:
  print("YES")
  exit()

p[err_idx[0]] = err[1]
p[err_idx[1]] = err[0]

for i in range(N):
  if (i+1)!=p[i]:
    print("NO")
    exit()
print("YES")

Solution (Python):
import sys

n = int(input())
p = list(map(int, input().split()))
p_copy = p.copy()

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

if sort_hantei(p):
    print('YES')
else:
    for i in range(n):
        for j in range(i+1, n):
            p_copy[i] = p[j]
            p_copy[j] = p[i]
            if sort_hantei(p_copy):
                print('YES')
                sys.exit()
            p_copy = p.copy()
    print('NO')

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

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())
li = list(map(int,input().split()))	

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

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

cnt = 0
for i in range(1, n):
    if i != p_li[i - 1] :
        cnt += 1
if cnt <= 2 :
    print("YES")
else :
    print("NO")

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

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
l = stdin.readline().rstrip().split()

n = 0
for i in range (len(l)):
  if int(l[i]) == i+1:
    n += 1
  else:
    n += 0
if n == N or n == N-2:
  print('YES')
else:
  print('NO')

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


for i in range(N-1):
    if P[i] > P[i+1]:
        tmp = P[P[i]-1]
        P[P[i]-1] = P[i]
        P[i] = tmp
        break

if P == sorted(P):
    print('YES')
else:
    print('NO')

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

ge_cnt = 0
diffs = []
for i in range(N-1):
    if a[i] > a[i+1]:
        ge_cnt += 1
        diffs.append(a[i+1] - a[i])

if ge_cnt == 0:
    print('YES')
elif ge_cnt == 2:
    if diffs[0] == diffs[1]:
        print('YES')
    else:
        print('NO')
else:
    print('NO')

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

m = 0

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


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

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

onceflag = 0

for i in range(n):
    if numbers[i] == i + 1:
        continue
    else:
        onceflag += 1

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

Solution (Python):
n = (int(input()))
a = list(map(int,input().split()))
b = sorted(a)
j=0
u = [x-y for (x,y) in zip(a,b)]
for a in u:
    if(a!=0):
        j = j+1
if(j>2):
    print("NO")
else:
    print("YES")

Solution (Python):
N=int(input())
P=[int(x) for x in input().split()]
 
N_J=list(range(1,N+1))
ans='NO'
for i in range(N):
    for j in range(N):
        P_temp=P.copy()
        p_i=P[i]
        p_j=P[j]
 
        P_temp[i]=p_j
        P_temp[j]=p_i
        
        if P_temp==N_J:
            ans='YES'
print(ans)

Solution (Python):
n = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst)
if lst==lst2:
  print("YES")
else:
  tmp = 0

  for i in range(n):
    if lst[i]!=lst2[i]:
      tmp+=1
      
  if tmp != 2:
    print("NO")
  else:
    print("YES")

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

q=[]
q=sorted(p)

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

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

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

flag = 0
ans = "NO"

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

if flag == 0 or flag == 2:
  ans = "YES"
  
print(ans)

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

count = 0

p_sort = sorted(p)

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

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

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

i=0
i_index=0
j=0
j_index=0
for a in range(1, n+1):
  if a != l[a-1] and i == 0:
    i = l[a-1]
    i_index = a - 1
  elif a != l[a-1] and j == 0:
    j = l[a-1]
    j_index = a - 1

if i==0 and j==0:
  print("YES")
else:
  l[i_index]=j
  l[j_index]=i
  if sorted(l) == l:
    print("YES")
  else:
    print("NO")

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
for k in range(n-1):
    if p[k+1]-p[k]!=1:
        break
    if k==n-2:
        print("YES")
        exit()
for i in range(n-1):
    for j in range(i+1,n):
        pn=p.copy()
        x=pn[i]
        pn[i]=pn[j]
        pn[j]=x
        for k in range(n-1):
            if pn[k+1]-pn[k]!=1:
                break
            if k==n-2:
                print("YES")
                exit()
print("NO")

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) 

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
p = LI()

p_sorted = sorted(p)

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

Solution (Python):

N=int(input())
p=list(map(int,input().split()))
count=0
origin=[j+1 for j in range(N)]
for i in range(N):
    if p[i]!=origin[i]:
        count=count+1
if count>=3:
    print("NO")
else:
    print("YES")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
tmp = p[:]
new_p = sorted(p)
ans = 'NO'
for i in range(n):
    for j in range(n):
        tmp = p[:]
        tmp[i], tmp[j] = tmp[j], tmp[i]
        if tmp == new_p:
            ans = 'YES'
print(ans)

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

for i in range(N):
    if p[i] != i+1:
        tmp = p[i]
        p[i], p[tmp-1] = p[tmp-1], p[i]
        break

if p == sorted(p):
    print("YES")
else:
    print("NO")

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

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

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

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

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

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

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

Solution (Python):
def check(i,j,number_list):
    number=[]
    for k in number_list:
        number.append(k)
    k=number[i]
    number[i]=number[j]
    number[j]=k
    flag=1
    for l in range(len(number_list)):
        if (int(number[l])==(l+1)):
            continue
        else:
            flag=0
            break
    return flag
N=int(input())
in_number_list=input().split(" ")
number_list=[]

for i in in_number_list:
    number_list.append(int(i))

flag=0

for i in range(N):
    for j in range(i,N):
        a=check(i,j,number_list)
        if(a==1):
            print("YES")
            flag=1
            break
    if(flag==1):
        break
if(flag==0):
    print("NO")

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

Solution (Python):



N = int(input())
p = [int(i) for i in input().split()]
q = [i+1 for i in range(N)]

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

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

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

count = []
tmp_num = 0

for p_n in p:
    dif = p_n - tmp_num
    if dif < 0:
        count.append(abs(dif))
    tmp_num = p_n

if len(count) == 0:
    print("YES")
elif len(count) == 2:
    if count[0] == count[1]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

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

cnt = 0
for i in range(N):
  if (A[i] != i+1): 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=[]
for i in range(1,n+1):
    q.append(i)

a=0
for i in range(n):
    if p[i]==q[i]:
        a+=1
    else:
        a+=0

if a==n or a==n-2:
    print('YES')
else:
    print('NO')

Solution (Python):

def main():
	N = int(input()) 
	p = [int(f) for f in input().split()]
	
	A = [f for f, i in zip(p, range(1, N+1)) if not f-i == 0]

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


if __name__ == "__main__":
	main()

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

psort = [0] * N
for i in range(N):
    psort[i] = p[i]
psort.sort()

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

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

Solution (Python):
n = int(input())
arr = [int(i) for i in input().split()]
f = 0
for i in range(n):
	if(arr[i]-1!=i):
		f+=1
if(f==2 or f==0):
	print("YES")
else:
	print("NO")

Solution (Python):
import copy

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

list_sorted = list.copy()
list_sorted.sort()

stock = 0
for i in range(n):
    if list[i] != list_sorted[i]:
        stock += 1

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

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

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

d = []

for i in range(N):
  d.append(p[i]-P[i])
  
D = d[:]
while 0 in D:
  D.remove(0)
  
if max(d) == 0:
  print('YES')
elif len(D) == 2:
  if D[0] == (-D[1]):
    print('YES')
  else:
    print('NO')
else:
  print('NO')

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

cnt = 0
for i, v in enumerate(p):
    if i+1 != v:
        cnt += 1

if cnt >= 3:
    print('NO')
else:
    print('YES')

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

A = []
for i in range(N):
    A.append(i+1)

count=0
for j in range(N):
    if A[j] != P[j]:
        count += 1

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

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

Solution (Python):
n = int(input())
d = input().split()
 



cnt = 1
a=[]
b=[]
for i in d:
    if(int(i) != cnt):
        a.append(d.index(i)+1)
        b.append(int(i))
        
    cnt += 1


if(len(a)==2 or len(a)==0):
    b.sort()
    if(a==b):
        print("YES")
    else:
        print("NO")
else:
    print("NO")

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

sortedP = sorted(P)
idx = []
for i in range(N):
    if P[i] != sortedP[i]:
        idx.append(i)
if len(idx) == 0:
    print("YES")
elif len(idx) == 2:
    P[idx[0]], P[idx[1]] = P[idx[1]], P[idx[0]]
    if P == sortedP:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
if P==list(range(1,n+1)):
    print('YES')
else:
    for i in range(n-1):
        for j in range(i,n):
            Q=P[:]
            Q[i]=P[j]
            Q[j]=P[i]
            if Q==list(range(1,n+1)):
                print('YES')
                exit()
    print('NO')

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

seq = map(int, input().split())

diff = 0

for i, v in enumerate(seq, start=1):
    if i != v:
        diff += 1

answer = "YES" if diff in (0, 2) else "NO"
print(answer)

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

cnt = 0
for i in range(n-1):
	if numbers[i] == i +1 :
		continue
	else:
		cnt += 1

if cnt > 2:
	result = "NO"
else:
	result = "YES"

print(result)

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

from copy import deepcopy

y=sorted(x)

res='NO'

if x==y:
	res='YES'
else:
	for i in range(n-1):
		for j in range(i+1,n):
			z=deepcopy(x)
			a=z[i]
			b=z[j]
			z[i]=b
			z[j]=a
			if z==y:
				res='YES'
				break
		if res=='YES':
			break

print(res)

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

count = 0
for i in range(n):
  if not p[i] == p2[i]:
    count += 1
  if count >= 3:
    print('NO')
    exit()
print('YES')

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==0 or c==2:
	print("YES")
else:
	print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
pp = p[:]
p.sort()
count = 0

for i in range(len(p)):
    if p[i] != pp[i]:
        count += 1
    else:
        pass

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

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

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


cnt = 0

s_1 = 0
for i in range(N):
    if P[i] != i + 1:
        if cnt == 1:
            print('NO')
            exit()

        s_1 = i

        for j in range(N):
            if P[j] == i + 1:
                s_2 = j

                v = P[s_1]
                P[s_1] = P[s_2]
                P[s_2] = v
                cnt += 1
                break
            else:
                continue

print('YES')

Solution (Python):
import copy

N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst)
sum = 0
for i in range(N):
  if (lst[i] != lst2[i]):
    sum += 1
if (sum == 0 or sum == 2):
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = list(filter(lambda i: a[i]!=b[i], range(n)))
print('YES' if len(c) in [0,2] else 'NO')

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

sabun = [P[i]-P_t[i] for i in range(N)]
c = N - sabun.count(0)
if c == 0 or c == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N=int(input())
p=list(map(int, input().split()))
import copy
new_p=sorted(p)
cnt=0
a=[]
for i in range(N):
    if p[i]==new_p[i]:
        pass
    else: 
        cnt+=1
        a.append(i)
if cnt==2:
    newnew_p=copy.copy(p)
    newnew_p[a[0]]=p[a[1]]
    newnew_p[a[1]]=p[a[0]]
    if newnew_p==new_p:
        print("YES")
    else: print("NO")
elif cnt==0:
    print("YES")
else: print("NO")

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

for x, y in zip(p, q):
    if x != y:
        count += 1
    else:
        pass

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

Solution (Python):
import numpy as np

N=int(input())
P=np.array(input().split(),dtype=np.int32)


if (np.sort(P) == P).all():
    print("YES")
else:
    sort_P=np.sort(P)
    count=0
    for i in range(N):
        if sort_P[i]!=P[i]:
            count+=1
    if count==2:
        print("YES")
    else:
        print("NO")

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
A_=sorted(A)
if A==A_:
  print("YES")
  exit()
for i in range(N):
  for j in range(i):
    B=A[::]
    B[i],B[j]=B[j],B[i]
    if B==A_:
      print("YES")
      exit()
print("NO")

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

Solution (Python):
N = int(input())
l_a = list(map(int,input().split()))
l_b = sorted(l_a)
if l_a[0] != 1:
    k = l_a.index(1)
    l_a[0],l_a[k] = l_a[k],l_a[0]
else:
    for i in range(1,N):
        if l_a[i] != l_a[i-1]+1:
            k = l_a.index(i+1)
            l_a[i],l_a[k] = l_a[k],l_a[i]
            break
if l_a == l_b:
    print('YES')
else:
    print('NO')

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

mis_match = sum([ 1 for s in p if not p.index(s) +1 == s])

judge="NO"
if mis_match in [0,2]:
    judge="YES"
print(judge)

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
c = o = 0
for i in range(n):
    if a[i] != i+1 and c == 0:
        c = 1
        o = a[i]
        if a[o-1] != i+1:
            print('NO')
            exit()
    elif a[i] != i+1 and c == 1:
        if i+1 != o:
            print('NO')
            exit()
print('YES')

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

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

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
cnt = 0

for i in range(len(L)):
    if L[i] != i+1:
        cnt += 1
if cnt >= 3:
    print('NO')
else:
    print('YES')

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

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

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

Solution (Python):


import os
import sys

N = int(input())

p = list(map(lambda x: int(x), input().split(" ")))

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

cnt = 0

for i in range(N):
	if p[i] != q[i]:
		cnt += 1
		if cnt > 2:
			break

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

Solution (Python):

def main():
    N = int(input())
    A = list(map(int,input().split()))
    count = 0
    
    for i in range(N):
        if (i+1) != A[i]:
            count += 1
    if count==0 or count == 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

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

sp = sorted(p)
mes = ""
cnt = 0

for i in range(n):
  if sp[i] != p[i]:
    cnt+=1
if cnt < 3:
  mes = "YES"
else:
  mes = "NO"
print(mes)

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

flag = 0
for i in range(n):
    for j in range(i+1, n):
        p[i], p[j] = p[j], p[i]
        for k in range(n):
            for l in range(k+1, n):
                if p[k] > p[l]:
                    flag = 1
                    break
        if flag == 0:
            print("YES")
            exit()
        p[i], p[j] = p[j], p[i]
        flag = 0

flag = 0
for i in range(n):
    for j in range(i+1, n):
        if p[i] > p[j]:
            flag = 1
if flag:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
s = list(map(int, input().split()))
index1 = 0
value1 = 0
index2 = 0
value2 = 0
count = 0

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

Solution (Python):

import numpy as np


def calc(N: int, values: np.ndarray) -> bool:
    sorted_values = np.sort(values)
    difference = sorted_values != values
    difference_count = difference.sum()
    return difference_count == 0 or difference_count == 2


def load_input_as_int_array() -> np.ndarray:
    return np.array([int(v) for v in input().split(" ")])


def main() -> None:
    N = load_input_as_int_array()
    values = load_input_as_int_array()
    result = calc(N, values)
    result_str = "YES" if result else "NO"
    print(result_str)


main()

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

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

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

tmp = N + 1
count = 0
ret = 1
for i in range(N):
    if p[i] == i + 1:
        continue
    else:
        count += 1
        if count == 3:
            ret = 0
            break
        elif count == 2:
            if tmp == i + 1:
                ret = 1
            else:
                ret = 0
                break
        tmp = p[i]

if ret == 1:
    print("YES")
else:
    print("NO")

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

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

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

r_count = 0
target_index_dict = {}
for i, number in enumerate(p_list):
    if i+1 != number:
        r_count += 1
        target_index_dict[i] = number

if not target_index_dict:
    ans = 'YES'
else:
    if len(target_index_dict) >= 3:
        ans = 'NO'
    else:
        a_key = list(target_index_dict.keys())[0]
        a_value = target_index_dict[a_key]
        b_key = list(target_index_dict.keys())[1]
        b_value = target_index_dict[b_key]
        if (a_key+1 == b_value) and (b_key+1 == a_value):
            ans = 'YES'

print(ans)

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

sort = sorted(ls)

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

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

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

up = sorted(p)
cntup = 0
for i in range(n):
  if(up[i] == p[i]):
    cntup += 1

if(cntup == n-2 or cntup == n):
  print("YES")
else:
  print("NO")

Solution (Python):
N = int(raw_input())
P = map(int, raw_input().split(' '))

count = 0

for i in range(N):
    if P[i] != i+1:
        count += 1
        
    if count > 2:
        print("NO")
        break
if count == 2 or count ==0:
    print("YES")

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

s = sorted(l)

ret = []
for i in range(len(l)):
    if l[i] != s[i]:
        if len(ret) >= 2:
            print("NO")
            exit(0)

        ret.append(i)

le = len(ret)
if le == 0 or  le == 2:
    print("YES")
    exit(0)

print("NO")

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

a = range(1, N+1)

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

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

Solution (Python):

n = int(input())
P = [int(el) for el in input().split(' ')]

count = 0
for p, q in zip(P, range(1,n+1)):
    if p != q:
        count += 1

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

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

P_sorted = sorted(P)


if P == P_sorted:
    print("YES")
else:
    count = 0
    for a, b in zip(P, P_sorted):
        if a != b:
            count += 1

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

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

Solution (Python):
import sys
N = int(input())
A = [int(i) for i in input().split()]
B = list(sorted(A))
if A == B:
    print("YES")
    sys.exit()
for i in range(N):
    for j in range(i,N):
        if i == j:
            continue
        A[i],A[j]=A[j],A[i]
        if A == B:
            print("YES")
            sys.exit()
        A[i],A[j]=A[j],A[i]
print("NO")

Solution (Python):
def checkSorted(n,l):
    f=0
    s=0
    count=0
    for i in range(1,n):
        if l[i]<l[i-1]:
            count+=1
            if f==0:
                f=i
            else:
                s=i
    if count>2:
        return 0
    if count==0:
        return 1
    if count==2:
        l[f-1],l[s]=l[s],l[f-1]
    elif count==1:
        l[f-1],l[f]=l[f],l[f-1]
    for i in range(1,n):
        if l[i]<l[i-1]:
            return 0
    return 1
n=int(input())
l=list(map(int,input().split()))
if checkSorted(n,l)==1:
    print("YES")
else:
    print("NO")

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

cnt=0
for i in range(n+1)[1:]:
  if i != a[i - 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()))
ps=sorted(p)
cnt=0
for i in range(N):
    if p[i]==ps[i]:
        cnt+=1

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

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

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

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
a = list(range(1, n+1))
c = 0
for pi, ai in zip(p, a):
    if pi != ai:
        c += 1
print('YES') if c <= 2 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 >= 3 :
    print('NO')
else :
    print('YES')

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

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

Solution (Python):

import sys;
import string;
sys.setrecursionlimit(20000) 

import copy
import math
import itertools

from functools import lru_cache


PPP = 1000000007


n = int(input())








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





























import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s





def getEnergy():
  for y in b:
    yield y

def doit():
  ng = 0
  for i in range(n):
    if i + 1 != p[i]:
      ng += 1
  
  print("", ng, file=sys.stderr)
  return ng == 0 or ng == 2

def yn():
  if doit():
    return "YES"
  else:
    return "NO"




try:
  
  
  print(yn())
except Exception as e:
  print("e", e, file=sys.stderr)

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 == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
B = list(map(int, input().split()))
b = sorted(B)
count = 0
for i in range(N):
    BB = B[i]
    bb = b[i]
    if BB != bb:
        count += 1
if count <= 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 or count == 0:
  print('YES')
else:
  print('NO')

Solution (Python):

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

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

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

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

for i in range(n):
    for j in range(n):
        p_tmp = p[:]
        tmp = p_tmp[i]
        p_tmp[i] = p_tmp[j]
        p_tmp[j] = tmp
        match = True
        for k in range(n):
            if p_tmp[k] != p_sort[k]:
                match = False
        if match:
            flag = True
            break
    if flag:
        break

if flag:
    print("YES")
else:
    print("NO")

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

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

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

Solution (Python):
def main():
    n = int(input())
    p = list(map(int,input().split()))
    neoP = sorted(p)
    cnt = 0
    for i in range(len(p)):
        if p[i] != neoP[i]:
            cnt += 1

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

main()

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
counter = 0
i = 1
for j in p:
    if j != i:
        counter += 1
    i += 1
if counter == 0 or counter == 2:
    print("YES")
else:
    print("NO")

Solution (Python):

N = int(input())

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



correct = range(1, N+1)

count = 0



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



print('NO') if count >= 3 else print('YES')