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()))
q=sorted(p)
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()))
ans = 0 
for i in range(n):
    if p[i] != i+1:
        ans += 1
    if ans >= 3:
        print("NO")
        exit()
print("YES")

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

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


flag=False
if pl==sorted(pl):
    flag=True
else:
    for i in range(n):
        for j in range(n):
            templ=pl[:]
            templ[i]=pl[j]
            templ[j]=pl[i]
            if templ==(sorted(pl)):
                flag=True
                break
if flag:
    print("YES")
else:
    print("NO")

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

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

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

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

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

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

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

Solution (Python):
n = int(input())
P = [int(x) for x in input().split()]
res = 0
for i, p in enumerate(P, 1):
  if not i == p:
    res += 1
print('YES' if res <= 2 else 'NO')

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

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

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
P = iil()
P_sorted = sorted(P)

indexes = []
count = 0

for i in range(n):
    if P[i] != P_sorted[i]:
        indexes.append(i)
        count += 1

if count == 0:
    print('YES')
elif count != 2:
    print('NO')
else:
    tmp = P[indexes[0]]
    P[indexes[0]] = P[indexes[1]]
    P[indexes[1]] = tmp

        
    if P != P_sorted:
        print('NO')
        exit()
    print('YES')

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

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

for i in range(n):
    for j in range(n):
        tmp = p.copy()
        tmp_i = tmp[i]
        tmp[i] = tmp[j]
        tmp[j] = tmp_i
        for k in range(n):
            cnt = 0
            if(ex[k] != tmp[k]):
                cnt += 1
                break
        if(cnt == 0):
            print('YES')
            exit()

print('NO')

Solution (Python):
import copy
N = int(input())
p = list(map(int,input().split()))
ph = copy.copy(p)
sp = copy.copy(p)
sp.sort()
for i in range(N-1):
  for j in range(i,N):
    tmp = ph[i]
    ph[i] = ph[j]
    ph[j] = tmp
    if sp == ph:
      print('YES')
      exit()
    ph = copy.copy(p)
print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

    s_p = sorted(p)

    c = 0
    for i in range(N):
        if p[i] != s_p[i]:
            c += 1
            if c > 2:
                print('NO')
                exit()

    print('YES')

if __name__ == "__main__":
    main()

Solution (Python):
import copy
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    s=LIST()
    sort_s=sorted(s)
    flg=0
    if s==sort_s:
        flg=1
    for i in range(n-1):
        for k in range(i+1,n):
            temp=copy.deepcopy(s)
            temp[i],temp[k]=temp[k],temp[i]
            
            if temp==sort_s:
                flg=1
    if flg==1:
        print('YES')
    else:
        print('NO')
        
    
if __name__ == '__main__':
    do()

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

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

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

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

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

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

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

if p == q:
  print("YES")

else:
  for i in range(n): 
    if p[i] != q[i]: 
      c = 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()))
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(x) for x in input().split() ]
q = sorted(p)

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

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

Solution (Python):
import sys
import copy
n = int(input())
p = list(map(int,input().split()))
flag = True
for i in range(1,n):
     if p[i] < p[i-1]:
          flag = False
          continue
if flag:
     print('YES')
     sys.exit()

for i in range(n-1):
     for j in range(i,n):
          pp = copy.deepcopy(p)
          pp[i] = copy.deepcopy(p[j])
          pp[j] = copy.deepcopy(p[i])
          flag = True

          for k in range(1, n):
               if pp[k] < pp[k - 1]:
                    flag = False
                    continue
          if flag:
               print('YES')
               sys.exit()

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

Solution (Python):
n = int(input())
l = [int(i) for i in input().split()]
mis = 0
for i in range(n):
    if l[i] != i+1:
        mis += 1
print('YES' if mis<3 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('NO')
else:
    print('YES')

Solution (Python):
N=int(input())
P=list(map(int,input().split()))
_=0
for i,p in enumerate(P):
  if i+1!=p:
    _+=1
if _<=2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
num = list(map(int, input().split()))
num_sort = sorted(num)
flug = 0
for i in range(n):
  if num[i] == num_sort[i]:
    continue
  else:
    flug += 1
    if flug <= 2:
      continue
    else:
      print("NO")
      exit()
print("YES")

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

Solution (Python):
import os, sys, re, math

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

diff = [P[i] != i + 1 for i in range(N)]
if sum(diff) == 2 or sum(diff) == 0:
    print('YES')
else:
    print('NO')

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

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

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

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

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
sortlst = [i + 1 for i in range(len(lst))]
non_match = 0
for a, b in zip(lst, sortlst):
	if a != b:
		non_match += 1
if non_match == 2 or non_match == 0:
	print('YES')
else:
	print('NO')

Solution (Python):
N = int(input())
P = [int(s) for s in input().split(' ')]
P2 = sorted(P)
ret = [1 for p, p2 in zip(P, P2) if p != p2]
print('YES' if len(ret) <= 2 else 'NO')

Solution (Python):
N = int(input())
P = [ int(s) for s in input().split() ]
P_S = sorted(P)
 
if sum(map(lambda x: x[0] != x[1], zip(P,P_S))) <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
import os, sys, re, math

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

sp = sorted(p)
diff = sum([1 if p[i] != sp[i] else 0 for i in range(N)])

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

Solution (Python):
n = int(input())
aaa = list(map(int, input().split(' ')))
correct_case = [i+1 for i in range(n)]
hoge = sum([a != num for a, num in zip(aaa, correct_case)])
print('YES' if hoge <= 2 else 'NO')

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
num = list(range(1,N+1))
ans = 0
for i in range(N):
    if p[i] != num[i]:
        ans += 1
if ans == 2:
    print('YES')
elif p == num:
    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] != i + 1:
        c = c + 1

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

Solution (Python):
import copy

n = int(input())
p = list(map(int, input().split()))
q = copy.copy(p)
q.sort()
c = 0

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

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

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

Solution (Python):

import sys
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('NO')
else:
    print('YES')

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

newp=sorted(p)
for i in range(len(newp)):
  ans1=p[i]
  ans2=newp[i]
  if ans1!=ans2:
    count+=1
    
if count==2 or newp==p:
  print("YES")
else:
  print("NO")

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

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

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

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

for i in range(N):
  if p[i] != i+1:
    res += 1
    
if res <= 2:
  print("YES")
else:
  print("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:
    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_sort = sorted(p)
non_0 = 0

for i in range(n):
    diff = p[i] - p_sort[i]
    if diff != 0:
        non_0 += 1
        if non_0 >2:
            print('NO')
            exit()

print('YES')

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

Solution (Python):

A = int(input())

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

A_list = []
for i in range(A):
  A_list.append(i+1)
  S_list[i] = int(S_list[i])
    

counter = 0
for i in range(A):
  if(A_list[i] != S_list[i]):
    counter = counter + 1

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

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

w1 = None
w2 = None
cnt = 0
for i in range(N):
  if i+1 != p[i]:
    cnt += 1
    if cnt == 1:
      w1 = i
    elif cnt == 2:
      w2 = i
    else:
      break
if cnt == 3:
  print('NO')
else:
  print('YES')

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:
    cnt+=1
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(1, N+1):
    if p[i-1]!=i:
        cnt += 1

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

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
P = np.array(lr())
Q = P.copy()
Q.sort()
result = P == Q
if N - result.sum() <= 2:
    print('YES')
else:
    print('NO')

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

res = sorted(p)
cnt = 0

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

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

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

Solution (Python):












n = int(input())

arrA = list(map(int,input().split()))
stdarr = list(range(1,n+1))

checknum = 0
for i in range(n):
    if arrA[i] != stdarr[i]:
        checknum += 1
    else:
        continue

if checknum <=2 and checknum!=1:
    ans = "YES"
else:
    ans = "NO"

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a_sort = sorted(a)
count = 0
ans = sum([0 if i==j else 1 for i,j in zip(a,a_sort)])
if 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 <=2:
    print("YES")
else:
    print("NO")

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
l_1 = sorted(l)
d = "NO"
for m in range(a):
  for n in range(a):
    x =l[m]
    l[m] = l[n]
    l[n] = x
    if l == l_1 :
      d ="YES"
      break
    else:
      x = l[n]
      l[n]=l[m]
      l[m]=x
print(d)

Solution (Python):
n = input()
l = map(int,raw_input().split())
sl = sorted(l)
count = 0
for i in xrange(n):
    if sl[i] != l[i]:
        count += 1
if count >2 :
    print "NO"
else:
    print "YES"

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

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

p_sorted = sorted(p_list)


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

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

Solution (Python):
n,*a=open(0)
a=list(map(int,a[0].split()))
b=sorted(a)
c=[a-b for a,b in zip(a,b)]
d=int(n)-c.count(0)
print("YES" if d==0 or d==2 else "NO")

Solution (Python):
n,p=open(0)
i=1
k=0
a='YES'
for j in p.split():
  if int(j)!=i:k+=1
  if k>2:a='NO'
  i+=1
print(a)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
a = list(range(1, int(n)+1))
cnt = sum(i != j for i, j in zip(p, a))
print('YES' if cnt == 0 or cnt == 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):
N=int(input())
p=list(map(int,input().split()))
A=sorted(p)
count=0
for i in range(len(p)):
    if p[i]!=A[i]:
        count=count+1
if count==0 or count==2:
    print("YES")
else:
    print("NO")

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

def is_in_ascending_order(p):
  res = True
  n = len(p)
  for i in range(n-1):
    if not p[i] <= p[i+1]:
      res = False
  return res

res = False
for i in range(N-1):
  for j in range(i+1, N):
    _p = p.copy()
    _p[i], _p[j] = _p[j], _p[i]
    if is_in_ascending_order(_p):
      res = True


if is_in_ascending_order(p):
  res = True

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

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

Solution (Python):
import copy

def swap(array, i,j,tar1,tar2):
  tmp = tar1 
  array[i] = tar2
  array[j] = tmp

  return array

def orderd(array):
  orderd_array = sorted(array)

  for i in range(len(array)):
    if not orderd_array[i]==array[i]:
      return False
  return True

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

ans = "NO"
flg = False
for i in range(N):
  for j in range(i,N):
    Pi = copy.copy(P)
    Pi = swap(Pi,i,j,Pi[i],Pi[j])
    if orderd(Pi):
      ans = "YES"
      flg=True
      break
  if flg:
    break

print(ans)

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

Solution (Python):
input();print('YNEOS'[sum(i+1!=int(j) for i,j in enumerate(input().split()))>2::2])

Solution (Python):
input();print('YNEOS'[sum(i+1!=int(j) for i,j in enumerate(map(int,input().split())))>2::2])

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

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

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

s = sorted(p)
ok = False
if s == p:
    ok = True
for i in range(len(p)):
    for j in range(i + 1, len(p)):
        q = p[:]
        q[i], q[j] = q[j], q[i]
        if q == s:
            ok = True
print('YES' if ok else 'NO')

Solution (Python):

"""
B - 0 or 1 Swap
https://atcoder.jp/contests/abc135/tasks/abc135_b

"""
import sys


def solve(N, arr):
    res = sum(a != b for a, b in zip(arr, sorted(arr)))
    return 'YES' if res <= 2 else 'NO'


def main(args):
    N = int(input())
    arr = [int(p) for p in input().split()]
    ans = solve(N, arr)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
N = int(input())
P = [int(p) for p in 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(n):
    if p[i] != i + 1:
        count += 1
if count > 2:
    print('NO')
else:
    print('YES')

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

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 'YES'
    c = 0
    for i, a in enumerate(A):
        if i+1 != a:
            c += 1
    
    return 'YES' if c == 0 or c == 2 else 'NO'

print(solve())

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

sorted_p = sorted(P)

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

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
s = sorted(p)
cnt = 0
ans = 'YES'

for (pi, si) in zip(p, s):
    if pi != si:
        cnt += 1

if cnt != 0 and cnt != 2:
    ans = 'NO'

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
pre = 0
onef = False
twef = False
badf = False
for i,num in enumerate(a):
    if i+1 != num:
        if not onef:
            onef = True
            badf = True
        elif not twef:
            twef = True
            badf = False
        else:
            badf = True
if not badf:
    print('YES')
else:
    print('NO')

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

P1 = sorted(P)

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

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

Solution (Python):
N = int(input())
P = input().split()
flag = "OK"
count = 0
for i in range(N):
  if i+1 != int(P[i]) :
    count = count + 1
if count >= 3:
  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 == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

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

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

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

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

    Count = 0
    for i, j in zip(ps, ps_sort):
        if i != j:
            Count += 1

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


if __name__ == "__main__":
    main()

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

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

Solution (Python):
N = int(input())
List = [int(n)-1 for n in input().split(" ")]

flag = 0
count = 0

for i in range(N):
    l = List[i]
    if l!=i:
        if List[l] == i:
            count+=1
            List[i] = i
            List[l] = l
        else:
            flag=1
            break
        if count > 1:
            flag=1
            break
if flag==0:
    print("YES")
else:
    print("NO")

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

Solution (Python):
n = int(input())
p_list = list(map(int, input().split()))
ans = 0
for i in range(1, n + 1):
    if p_list[i - 1] != 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()))

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

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

Solution (Python):
import copy

n = int(input())
p = list(map(int, input().split()))
p_new = copy.copy(p)
for i in range(n-1):
  if p[i] > p[i+1]:
    min_val = min(p[i+1:])
    for j in range(n):
      if p[j] == min_val:
        p_new[i] = min_val
        p_new[j] = p[i]
        break
    break

if p_new == sorted(p):
  print('YES')
else:
  print('NO')

Solution (Python):
N = int(input())
number = input().split(" ")
ans = 0
for i in range(1,N+1):
  if str(i) == str(number[i-1]):
    pass
  else:
    ans += 1
if ans <= 2:
  print("YES")
else:
  print("NO")

Solution (Python):

n = int(input())
p = list(map(int,input().split()))
q = p.copy()
q.sort()
status = 0

for i in range(n):
    for j in range(n):
        r = []
        r = p.copy()
        if i == j:
            pass
        else:
            tmp = r[i]
            r[i] = r[j]
            r[j] = tmp

        if q == r:
            status = 1
            break

if status == 0:
    print("NO")
else:
    print("YES")

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
x=[i+1 for i in range(n)]
if p==x:print("YES");exit()
for i in range(n):
  for j in range(n):
    if i==j:continue
    p[i],p[j]=p[j],p[i]
    if p==x:print("YES");exit()
    p[i],p[j]=p[j],p[i]
print("NO")

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

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

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

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

Solution (Python):


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

wrong_order = 0

for i in range(n):
    if P[i] != i + 1:
        wrong_order += 1

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

Solution (Python):
import math
import sys

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

s = 0 
for i in range(0,N):
    if i+1 != P[i]:
        s = s + 1
        if s > 2:
            print('NO')
            exit(0)

print('YES')

Solution (Python):

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

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

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

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
sort_p = sorted(p)
ans = "YES"
c = 0
for n,m in zip(p,sort_p):
  if n != m:
    c += 1
if c > 2:
  ans = "NO"
print(ans)

Solution (Python):
import sys
import math
'''a,b= map(int,input().split())'''


a=int(input())
li0= [int(x) for x in input().split()]
count=0
for i in range(1,1+a):
  if i!=li0[i-1]:
    count+=1
if count<=2:
  print("YES")
else:
  print("NO")

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

l = [i != pi for i, pi in zip(range(1, N+1), p)]
if sum(l) <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]
pn = [int(x) for x in range(1, n+1)]

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

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
sort_p = sorted(p)
ans = "YES"
c = 0
for n,m in zip(p,sort_p):
  if n != m:
    c += 1
if c > 2:
  ans = "NO"
print(ans)

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

Solution (Python):

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


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

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


p_ans = sorted(p)

cnt=0
for i in range(len(p)):
    if p[i] == p_ans[i]:
        pass
    else:
        cnt+=1

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

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

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

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

Solution (Python):
import sys

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

count = 0
for i in range(N):
  if p[i] != i+1 : count+=1
print('YES' if count < 3 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):
n=int(input())
l=list(map(int,input().split()))
k=[]
for i in range(1,n+1):
    k.append(i)

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

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

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

p_s = sorted(p)

b = True
cnt = 0

for p1, p2 in zip(p, p_s):
    if p1 != p2:
        cnt += 1
    
    if cnt >= 3:
        b=False
        break

if b:
    print('YES')
else:
    print('NO')

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

mx = 0
for k in range(n):
    pk = p[k]
    mx = max(mx, pk)
    if pk < mx:
        break
else:
    print('YES')
    sys.exit()

for i in range(n):
    for j in range(i+1,n):
        mx = 0
        for k in range(n):
            pk = p[k]
            if k == i: pk = p[j]
            if k == j: pk = p[i]
            mx = max(mx, pk)
            if pk < mx:
                break
        else:
            print('YES')
            sys.exit()
print('NO')

Solution (Python):
import sys
def chk(v):
  for i in range(1,len(v)):
    if v[i-1]>v[i]:
      return False
  return True
n=int(input())
p=list(map(int,input().split()))
if chk(p):
  print("YES")
  sys.exit()
for i in range(n):
  for j in range(i+1,n):
    
    v=[]
    for k in p:
      v.append(k)
    v[i],v[j]=v[j],v[i]
    if chk(v):
      print("YES")
      sys.exit()
print("NO")