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(input())
p = list(map(int,input().split()))
for i in range(N):
    if p[i] != i+1:
        a = p[i]
        p[i] = i+1
        p[a-1] = a
        break
if p == [i for i in range(1,N+1,1)]:
    print("YES")
else:
    print("NO")

Solution (Python):

N = int(input())
p = list(map(int,input().split()))
for i in range(N):
    if p[i] != i+1:
        a = p[i]
        p[i] = i+1
        p[a-1] = a
        break
if p == [i for i in range(1,N+1,1)]:
    print("YES")
else:
    print("NO")

Solution (Python):
import numpy as np
N=int(input())
list_p=list(map(int,input().split()))
np_list_p=np.array(list_p)
np_list_s =np.array(sorted(list_p))
minus=list(np_list_p - np_list_s)
print("YES" if minus.count(0) == N or minus.count(0)== N-2 else "NO")

Solution (Python):
n = int(input())
lista = [int(i) for i in input().split()]
a=0
for i in range(n):
    if (i+1)!=lista[i]:
        a=a+1

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

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
sortP = sorted(P)
cnt = 0
for p, sp in zip(P, sortP):
  if p != sp: cnt += 1

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

Solution (Python):



N = int(input())
plist = tuple(map(int,input().split()))
answer_p = list(plist)
answer_p.sort()
able = False

for i in range(N-1):
    if able == True:
        break
    p = list(plist)
    j = i + 1
    for j in range(N):
        if able == True:
            break
        p = list(plist)
        p[i] = plist[j]
        p[j] = plist[i]
        if p == answer_p:
            print('YES')
            able = True

if able == False:
    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()))
dic = {}
for i in range(1, N+1):
    dic[i] = p[i-1]
 
cnt = 0
for key,val in dic.items():
    if key!=val:
        cnt += 1
 
if cnt==2 or cnt==0:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
N=list(range(1,n+1))
count=0
for x,y in zip(p,N):
    if x-y!=0:
        count+=1
print('YES' if count<=2 else 'NO')

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
false_count = 0
for index in range(len(a_list)):
  if index + 1 != a_list[index]:
    false_count += 1

if false_count in [0, 2]:
  print("YES")
else:
  print("NO")

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

ans = 0
for i in range(len(a)):
  if i != a[i]-1:
    ans += 1
    
if ans <= 2:
  print("YES")
else:
  print("NO")

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

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

Solution (Python):
n,*p = map(int,open(0).read().split())
ans = 0
for i in range(n):
    if i+1 != p[i]:
        ans += 1
print('YES' if ans == 2 or ans == 0 else 'NO')

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]

def check():
    cs = []
    for i in range(N):
        if A[i] != i + 1:
            cs.append(i)
    if len(cs) == 0:
        return True
    if len(cs) != 2:
        return False
    return True

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

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

for i_p, i_pa in zip(p, p_asc):
    if i_p != i_pa:
        diff += 1
    if diff > 2:
        print("NO")
        exit()
print("YES")

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

t = sorted(p)

count = 0

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

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

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

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

z = 0

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

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

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

lis_ans = sorted(lis_p)

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

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

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

z = 0

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

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

Solution (Python):
N = int(input())
n = range(1, N+1)
p = list(map(int, input().split()))

ans = 0
for i in range(N):
    if n[i] != p[i]:
        ans += 1
if ans == 0 or ans ==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 == 0 or count == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
s=input()
t=list(map(int,input().split()))
list=[]
a=int(s)
for j in range(a):
  list.append(t[j])
list.sort()
c=0
for i in range(a):
  if t[i]!=list[i]:
    c = c-1
if c<-2:
  print('NO')
else:
  print('YES')

Solution (Python):

import sys
import numpy as np


def solve(N: int, p: "List[int]"):
    sorted_list = [i for i in range(1, N + 1)]
    if N - sum(np.array(p) == sorted_list) == 0:
        print("YES")
    elif N - sum(np.array(p) == sorted_list) == 2:
        print("YES")
    else:
        print("NO")


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(N)]  
    solve(N, p)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
import numpy as np


def solve(N: int, p: "List[int]"):
    sorted_list = [i for i in range(1, N + 1)]
    if p == sorted_list:
        print("YES")
    elif N - sum(np.array(p) == sorted_list) == 2:
        print("YES")
    else:
        print("NO")


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(N)]  
    solve(N, p)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n, *p = map(int, open(0).read().split())

    cnt = 0
    for i, cur_p in enumerate(p):
        if i + 1 != cur_p:
            cnt += 1
        if cnt > 2:
            print('NO')
            return()
    print('YES')
    return()

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, *p = map(int, open(0).read().split())

    p_sort = sorted(p)

    cnt = 0
    for cur_p, cur_p_sort in zip(p, p_sort):
        if cur_p != cur_p_sort:
            cnt += 1
        if cnt > 2:
            print('NO')
            return()
    print('YES')
    return()

if __name__ == '__main__':
    main()

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

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

Solution (Python):
n=int(input())
p=[int(x) for x in input().split()]
if sum([p[i]!=i+1 for i in range(n)])<3:
  print('YES')
else:
  print('NO')

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

Solution (Python):
import numpy as np
N = int(input())
p= list(map(int, input().split()))
cnt = 0
org=np.arange(1,N+1,1)
for n in range(N):
    if org[n] != p[n]:
        cnt += 1
print('NO' if cnt>2 else 'YES')

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

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

Correct = []
for i in range(N):
  Correct.append(i+1)
  
count = 0
for i in range(N):
  if(Correct[i] != P[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()))
s = list(range(1,N+ 1))
count = 0
for i in range(N):
  if s[i] != p[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()))

count = 0

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

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

p_sort = sorted(p)
count = 0

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

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

Solution (Python):
N = int(input())
p = list(map(int , 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("YES")
else:
    print("NO")

Solution (Python):
import copy
n = int(input())
p = list(map(int, input().split()))
p_sorted = sorted(copy.copy(p))
flag = False
for i in range(n):
    swap = p[i]
    for j in range(n):
        pre_p = copy.copy(p)
        p[i] = p[j]
        p[j] = swap
        if p == p_sorted:
            flag = True
        p = pre_p
if flag:
    print("YES")
else:
    print("NO")

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

counter = 0

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

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

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

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
foundpair=False
foundi=False
sortedp=sorted(p)
pair=[]
if p==sortedp:
  print("YES")
  exit(0)
for i in range(N):
  if p[i]== sortedp[i]:
    continue
  else:
    if foundpair==False:
      if foundi:
        foundpair=True
        pair.append(i)
      else:
        foundi=True
        pair.append(i)
    else:
      print("NO")
      exit(0)
      
p[pair[0]],p[pair[1]]=p[pair[1]],p[pair[0]]
if p==sortedp:
  print("YES")
else:
  print("NO")

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

def is_sorted(array):
    v0 = array[0]
    for i in range(1, len(array)):
        v = array[i]
        if v0 > v:
            return False
        v0 = v
    return True
    
if is_sorted(P):
    print('YES')
    exit()

for i in range(0, N - 1):
    for j in range(i + 1, N):
        tmp = P[i]
        P[i] = P[j]
        P[j] = tmp

        if is_sorted(P):
            print("YES")
            exit()

        tmp = P[i]
        P[i] = P[j]
        P[j] = tmp

print("NO")

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

r = "YES"
cnt = 0
for i in range(n - 1):
	if(s[i + 1] - s[i] < 1):
		cnt += 1
		if(cnt == 1):
			a_value = s[i]
			a_index = i
		elif(cnt == 2):
			b_value = s[i + 1]
			b_index = i + 1
		elif(cnt > 2):
			r = "NO"
			break

if(cnt == 1):
	r = "NO"
elif(cnt == 2):
	s[a_index] = b_value
	s[b_index] = a_value
	if(s[a_index + 1] - s[a_index] < 1) or (s[b_index] - s[b_index - 1] < 1):
		r = "NO"

print (r)

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

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

Solution (Python):
N=int(input())
P=list(map(int,input().split()))
P_answer=[i+1 for i in range(N)]
swap=[0 for i in range(N)]
k=0

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

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

Solution (Python):
n=int(input())
x=list(input().split())
for i in range(n):
  x[i]=int(x[i])
xsort=sorted(x)
flag=0
for i in range(n):
  if x[i]!=xsort[i]:
    flag+=1

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

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

B = copy.deepcopy(A)
B.sort()
count = 0
for i, j in zip(A, B):
  if i != j:
    count += 1

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

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

tmp=0
s=0
cnt=0
e=0 

for i in range(1,n+1):
   
   if p[i-1] != 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
for i in range(1,n+1):
    if p[i-1] == i:
        count += 1
        
if n - count <= 2:
    print("YES")
else:
    print("NO")

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

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

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

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

LL = sorted(L)

count = 0

for i in range(N):
    if L[i]==LL[i]:
        pass
    else:
        count += 1

ans = 'YES' if count<=2 else 'NO'
print(ans)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
flag=False
ans_flag=True
for i in range(1,n):
  if(a[i-1]>a[i]):
    for j in range(n-2,-1,-1):
      if(a[j]>a[j+1]):
        tmp=a[i-1]
        a[i-1]=a[j+1]
        a[j+1]=tmp
        flag=True
        break
  if(flag==True):
    break
for i in range(n-1):
  if(a[i]>a[i+1]):
    print("NO")
    ans_flag=False
    break
if(ans_flag==True):
  print("YES")

Solution (Python):


N=int(input())
p=[int(i) for i in input().split()]
ch=[]

def judge():
    flag = 1
    for j in range(N):
        if p[j] == j+1:
            continue
        else:
            ch.append(j)
            flag = 0
    return flag,ch

flag,ch = judge()

if flag == 0:
    temp = p[ch[0]]
    p[ch[0]] = p[ch[1]]
    p[ch[1]] = temp

flag,ch = judge()

if flag== 1:
    print('YES')
else:
    print('NO')

Solution (Python):
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

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

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

dif = 0
for i in range(len(a)):
    if a[i] != b[i]:
        dif +=1

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

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

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

count = 0
for i, pi in enumerate(p):
    if (i+1) != pi:
        count+=1
    if count > 2:
        break

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

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

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

Solution (Python):
N = int(raw_input())
x = map(int, raw_input().strip().split())
y = sorted(x)
c = len([i for i in xrange(N) if x[i] != y[i]])
print "YES" if c <= 2 else "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("YES")
else:
    print("NO")

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
P = [int(_) for _ in stdin.readline().rstrip().split()]
target = 0
for i in range(N):
    if P[i] != i+1:
        target += 1 
if target <= 2:
    print('YES')
else:
    print('NO')

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

Solution (Python):
n = int(input())
p = list(int(i) for i in input().split()) 
q = sorted(p)

cnt = 0
for x in range(n):
    
    if(p[x] != q[x]):
        cnt += 1
    if(cnt>2):
        print("NO")
        exit()

print("YES")

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

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

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

Solution (Python):
N = int(input())
Pi = list(map(int, input().split()))
sortedPi = sorted(Pi)
wrongPiCnt = 0
for i in range(N):
    if Pi[i] - sortedPi[i] != 0:
        wrongPiCnt +=1
if wrongPiCnt > 2:print('NO')
else:print('YES')

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

cnt = 0
index = 0
nl = list(range(1, n + 1))
for pi in p:
  if nl[index] != pi:
    cnt += 1 
  index += 1

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

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

cnt =0

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

Solution (Python):


def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    counter = 0

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

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


if __name__ == "__main__":
    main()

Solution (Python):

n = int(input())
p = list(map(int,input().split()))
k = 0

for i in range(len(p)):
    if p[i]!=i+1:
        k+=1
if(k<=2):
    print("YES")
elif(k>=3):
    print("NO")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ref = list(range(1, n + 1))
res = 0

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

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

Solution (Python):

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

swap = 0

for i in range(N-1):
    if list_p[i] != i+1:
        swap+=1
        if swap>2:
            print("NO")
            break
else:
    print("YES")

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

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

Solution (Python):
n = int(input())
correct = list(range(1 , n + 1))
a = [int(_) for _ in input().split()]
wrong = 0
if n == len(a):
    for i in range(len(correct)):
        if a[i] != correct[i]:
            wrong += 1
    if 0 <= wrong <= 2:
        print('YES')
    else:
        print('NO')
else:
    print('hoge!')

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

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

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

list_correct = list(range(1, len(list_N)+1))

score = 0
for i in range(n):
    if list_correct[i] != list_N[i]:
        score = score + 1

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

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

Solution (Python):

from copy import deepcopy


def main():
    n = int(input())
    p = list(map(int, input().split()))

    for i in range(n):
        for j in range(i, n):
            
            
            p_temp = deepcopy(p)
            temp = p_temp[i]
            p_temp[i] = p_temp[j]
            p_temp[j] = temp
            
            if p_temp == sorted(p):
                print('YES')
                return

    print('NO')


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
import copy

N = int(input())
p_list = list(map(int, input().split()))
sort_list = copy.copy(p_list)
sort_list.sort()
p_i = -1
for k in range(N):
    if p_list[k] != k+1:
        if p_i < 0:
            p_i = k
        else:
            p_list[p_i], p_list[k] = p_list[k], p_list[p_i]
            break
if p_list == sort_list:
    print('YES')
else:
    print('NO')

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

cnt =0
check=0

for x in p:
    
    
    cnt += 1
    
    if x == cnt:
        continue
    else:
        check += 1




if check==0 or check==2:
    print('YES')
    
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()))

count=0

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

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

n = len(a)
if n==0 or n ==2:
	print("YES")

else:
	print("NO")

Solution (Python):
N=int(input())
list1=list(map(int,input().split()))
list2=[]
for i in range(1,N):
  list2.append(i)
a=0
for j in range(0,N-1):
  if not list1[j]==list2[j]:
    a+=1
if a>2:
  print("NO")
else:
  print("YES")

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

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

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

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

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

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

nums_ans=sorted(nums)
dif_count=0
for i in range(n):
    if nums[i] != nums_ans[i]:
        dif_count+=1
if dif_count <= 2:
    print("YES")
else:
    print("NO")

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

count = 0

for i in range(n):
    if i+1 != int(p_list[i]):
        count = 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)
cnt=0
for i in range(n):
    if p[i]!=p_sort[i]:
        cnt+=1
print('YES' if cnt==2 or cnt==0 else 'NO')

Solution (Python):
n = int(input())
ps = input().split()
ps = list(map(int, ps))
diffs = 0
for i in range(n):
    if i + 1 != ps[i]:
        diffs += 1
if diffs > 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
    else:
        cnt+=0
if cnt==0 or cnt ==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 == 0 or cnt == 2 :
  print("YES")
else:
  print("NO")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
same = 0
for i,v in enumerate(a):
    if v==i+1:
        same +=1
if same>=n-2:
    print("YES")
else:
    print("NO")

Solution (Python):
import numpy as np


N = int(input())
P = np.array(input().split(), dtype=np.int32)
P_sort = np.sort(P)
diff_P = P != P_sort
diff_num = np.count_nonzero(diff_P)

if diff_num == 0:
    print('YES')
elif diff_num == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys

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


sorted_p = sorted(p)

limit = 2

index_L,value_L = [],[]
for val in range(0,n):
    if p[val] != sorted_p[val]:
        index_L.append(val)
        value_L.append(p[val])
        limit -= 1

        if limit <= 0:
            p[index_L[0]] = value_L[1]
            p[index_L[1]] = value_L[0]

            if p == sorted_p:
                print("YES")
            else:
                print("NO")
            
            sys.exit(0)

print("YES")

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
pp=sorted(p,reverse=False)
ans=0
for i,j in enumerate(pp):
    if j!=p[i]:
        ans+=1
if ans==0 or ans==2:
    print("YES")
else :
    print("NO")

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

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ps = sorted(p)
x = 0
for i, j in zip(p, ps):
    if i != j:
        x += 1
if x > 2:
    print('NO')
else:
    print('YES')

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

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

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

if q <=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 i+1!=P[i]:
		cnt+=1
print('YES' if cnt==2 or cnt==0 else 'NO')

Solution (Python):
import copy

n = int(input())
P = list(map(int, input().split()))
P2 = copy.deepcopy(P)
P2.sort()

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

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

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

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

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

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

P_sorted = sorted(P)

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

    if count > 2:
        print("NO")
        exit()

print("YES")

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

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

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

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

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

Solution (Python):
import numpy as np
n = int(input())
p = [int(i) for i in input().split(" ")]
c = [0 if p[i]==i+1 else 1 for i in range(n)]
if np.sum(c) >= 3:
    print("NO")
else:
    print("YES")

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

n_error = 0

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

if n_error==0 or n_error==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 == 3:
            print('NO')
            exit()
print('YES')

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

p=list(map(int,input().split()))
sort=list(range(1,N+1))
count=list(p[i]==sort[i] for i in range(N)).count(False)
if count > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
def main(): 

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

    cnt = 0

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

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


if __name__ == "__main__":
    main()

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  

def solve(N: int, p: "List[int]"):
    
    q = sorted(p)
    x = [ p[i] == q[i] for i in range(N)]
    if(x.count(False) in (0,2)):
        print(YES)
    else:
        print(NO)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    p = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, p)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
p_lst = [int(x) for x in stdin.readline().rstrip().split()]

if sorted(p_lst) == p_lst:
  print("YES")
else:
  for i in range(n):
    for j in range(n):
      if i < j:
        p_lst_tmp = p_lst.copy()
        tmp = p_lst_tmp[i]
        p_lst_tmp[i] = p_lst_tmp[j]
        p_lst_tmp[j] = tmp
        if sorted(p_lst_tmp) == p_lst_tmp:
          print("YES")
          exit()
  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
print("YES" if cnt <= 2 else "NO")

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

cnt = 0
for i, p in enumerate(ps):
  if i + 1 != p:
    cnt += 1
    
print('YES' if cnt <= 2 else 'NO')

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

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