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

Solution (Python):
import numpy as np

N = int(input())
p_array = np.array(list(map(int, input().split())))

sorted_p = np.sort(np.copy(p_array))
diff = (p_array != sorted_p).sum()

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

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
isMatch = list(map(lambda x, y: x == y, p, sorted(p)))
result = 'YES' if isMatch.count(False) <= 2 else 'NO'
print(result)

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

count = 0
for i, j in zip(p, q):
    if i != j:
        count += 1
        if count > 2:
            print("NO")
            sys.exit()
print("YES")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
l = []
for i in range(n):
    p[i] -= 1
    if p[i] != i:
        l.append([i, p[i]])
if len(l) == 0:
    print('YES')
elif len(l) == 2 and l[0][1] == l[1][0] and l[0][0] == l[1][1]:
    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
print("YES" if count <= 2 else "NO")

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
S = [i for i in range(1, n + 1)]
ans = "YES"
if P != S:
  ans = "NO"
  for i in range(n):
    for j in range(i + 1, n):
      P[i], P[j] = P[j], P[i]
      if P == S:
        ans = "YES"
        break
      P[i], P[j] = P[j], P[i]
print(ans)

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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

p_sorted = sorted(p)
swap_pre = []

for i,j in zip(p, p_sorted):
    if i != j and not swap_pre:
        swap_pre += [i,j]
    elif i != j and swap_pre == [j, i]:
        pass
    elif i != j:
        print('NO')
        break
else:
    print('YES')

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

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

Solution (Python):
n = int(input())
l = [i - 1 for i in map(int, input().split())]
cnt = 0
for j, k in enumerate(l):
    if j != k:
        cnt += 1
if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
l = [i - 1 for i in map(int, input().split())]
cnt = 0
for j, k in enumerate(l):
    if j != k:
        cnt += 1
if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

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

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

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

def swap(list, pos1, pos2) :
  list[pos1], list[pos2] = list[pos2], list[pos1]
  return list

ok = 0
for i in range(n):
  for j in range(i, n):
    b = a.copy()
    swap(b, i, j)
    flag = 1
    for k in range(1, n):
      if b[k - 1] > b[k]:
        flag = 0

    if flag == 1:
      ok = 1

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

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

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

s = 0
A = [i for i in range(1, N+1)]

for i in range(N):
    if P[i] != A[i]:
        s += 1
        
print('NO' if s > 2 else 'YES')

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
A = []
for n in range(N):
  A.append(n+1)

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

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

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

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

Solution (Python):
N=int(input())
P=list(map(int,input().split()))
Q=sorted(P)
res=0
for i in range(N):
    if P[i]==Q[i]:
        res+=1
if res==N:
    print('YES')
else:
    print('YNEOS'[res!=N-2::2])

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

for i in range(n):
    for j in range(i,n):
        q = []
        for x in p:
            q.append(x)
        q[i],q[j] = q[j],q[i]
        sw = 1
        for k in range(n-1):
            if q[k] > q[k+1]:
                sw = 0
                break
        if sw == 1:
            ans = 1
        q = []
print(["NO","YES"][ans])

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ps=sorted(p)
b=[]
for i in range(n):
  b.append(p[i]-ps[i])
if b.count(0)<n-2:
  print('NO')
else:
  print('YES')

Solution (Python):
import sys

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


def solve():

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

    count = 0
    l = []
    for i in range(N):
        if p[i] == i + 1:
            continue
        else:
            count += 1
            l.append((i + 1, p[i]))

    if count == 0:
        ans = 'YES'
    elif count == 2:
        if l[0][0] == l[1][1] and l[1][0] == l[0][1]:
            ans = 'YES'
        else:
            ans = 'NO'
    else:
        ans = 'NO'

    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
N=int(input())
S=list(map(int,input().split()))
Z=[]
tmp=[]
for i in range(N):
    Z.append(S[i])
    tmp.append(S[i])
    

Z.sort()


for i in range(N):
    for j in range(N):
        
        R=tmp[i]
        tmp[i]=tmp[j]
        tmp[j]=R
        if tmp==Z:
            print("YES")
            exit()
        R=tmp[i]
        tmp[i]=tmp[j]
        tmp[j]=R        
        

print("NO")

Solution (Python):
def actual(n, P):
    
    n_unmatched = [i == p for i, p in enumerate(P, start=1)].count(False)

    if n_unmatched <= 2:
        return 'YES'

    return 'NO'


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

print(actual(n, P))

Solution (Python):
import copy


def actual(n, P):
    nums_asc = sorted(P)

    if nums_asc == P:
        return 'YES'

    N = len(P)
    for i in range(N):
        for j in range(1, N):
            swapped_nums = copy.deepcopy(P)

            swapped_nums[i] = P[j]
            swapped_nums[j] = P[i]

            if swapped_nums == nums_asc:
                return 'YES'

    return 'NO'

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

print(actual(n, P))

Solution (Python):
N=int(input())
P=list(map(int,input().split()))
Q=sorted(P)
res=0
for i in range(N):
    if P[i]==Q[i]:
        res+=1
if res==N:
    print('YES')
else:
    print('YNEOS'[res!=N-2::2])

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = [i for i in range(1, n + 1)]
k = 0
for p, q in zip(a, b):
  if p != q:
    k += 1
if k >= 3:
  print('NO')
else:
  print('YES')

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

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

Solution (Python):
def linp():return list(map(int,input().split()))
n=int(input())
p=linp()
s=0
for i in range(n):
    if i==p[i]-1:continue
    else:
        p[p[i]-1]=p[i]
        s+=1
if s<=1:print("YES")
else:print("NO")

Solution (Python):
import numpy as np

n = int(input())
p = np.array([int(i) for i in input().split()])

a = np.arange(-1, -n-1, -1)
print('YES' if np.count_nonzero(p+a) < 3 else 'NO')

Solution (Python):
def resolve():
    N = int(input())
    nom = list(map(int, input().split()))
    mon = sorted(nom)
    count = 0
    for i in range(N):
        if nom[i] != mon[i]:
            count += 1
    print("NO" if count >= 3 else "YES")
resolve()

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

ans=0
for i ,j in zip(l,lis):
   
    if i!=j:
        ans+=1
if ans<=2:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
ans=0
kai=0
ok=True
for i in range(n-1):
    if a[i]!=i+1:
        kai+=1
        if a[a[i]-1]!=i+1 or kai==3:
            ok=False
if ok:
    print("YES")
else:
    print("NO")

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

Solution (Python):
input()
p = [int(x) for x in input().split()]
q = sorted(p)
d = [(pi, qi) for pi, qi in zip(p, q) if pi != qi]
print("YES" if len(d) == 0 or len(d) == 2 and d[0][0] == d[1][1] and d[1][0] == d[0][1] else "NO")

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




p_s = sorted(p)



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


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

Solution (Python):
import copy

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

tlis = copy.deepcopy(lis)

sortlis = sorted(lis)


for i in range(n-1):
    for j in range(i,n):
        tmp = tlis[i]
        tlis[i] = tlis[j]
        tlis[j] = tmp
        if tlis == sortlis:
            print('YES')
            exit()
        else:
            tlis = copy.deepcopy(lis)
            
print('NO')

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    p = list(map(int, input().split()))
    count = 0
    for p, q in zip(p, sorted(p)):
        if p != q:
            count += 1

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


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()

def main():
    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")
                return
    if count == 2 or count == 0:
        print("YES")
        return
    print("NO")
        
        
    
main()

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

p_sorted = sorted(p)

is_changed = [0 if x==y else 1 for x,y in zip(p, p_sorted)]

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

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

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

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

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

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

Solution (Python):
def main():
    _ = int(input())
    A = list(map(int, input().split()))
    A_ = sorted(A)
    cnt = 0
    for i in range(len(A)):
        a = A[i]
        a_ = A_[i]
        if a != a_:
            cnt += 1

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


if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):


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

count = 0

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

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

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

def main():
    n = int(input())
    p_list = list(map(int, input().split()))
    count = 0
    for i in range(n):
        if i+1 != p_list[i]:
            count += 1
    
    if count >= 3:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

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

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

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

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

check = []

for x in range(N):
    check.append(x + 1)

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

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

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    p = LI()
    cnt = 0
    for i in range(N):
        if p[i] != i + 1:
            cnt += 1
    if cnt > 2:
        print("NO")
    else:
        print("YES")


main()

Solution (Python):

n = int(input())
p = list(map(int, input().split()))
q = [0]*n
for i in range(n):
    q[i] = p[i] - i - 1
if q.count(0) >= n - 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(n):
    if P[i]!=A[i]:
        count+=1
print("YES" if count<=2 else "NO")

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

a = sorted(p)
ans = 0

for i in range(n):
    if p[i] != a[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()))
temp=sorted(p)
ans=0
for i in range(n):
  if p[i]!=temp[i]:
    ans=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 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())))

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

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

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

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

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

p2 = sorted(p)

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

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

Solution (Python):



def main():
    N = int(input())
    ps = list(map(int, input().split()))
    ps = [0] + ps

    cnt = 0
    for i in range(1, N + 1):
        if ps[i] == i:
            continue
        if ps[ps[i]] == i:
            cnt += 1
        else:
            print('NO')
            return
    if cnt <= 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

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

cnt = 0
for i, j in enumerate(p):
    if i + 1 != j:
        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(1,n+1):
    if i == p[i-1]:
        pass
    else:
        count += 1
        if count ==3:
            print('NO')
            exit(0)
print('YES')

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(A):
    if i+1 != a:
        cnt += 1

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

Solution (Python):
n=int(input())
l=[int(x) for x in input().split()]
cnt=0
x=0
y=0
b=0
for i in range(n):
    if(i+1!=l[i]):
        cnt+=1
if(cnt>2):
    print("NO")
else:
    print("YES")

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

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)








n=n0()
p=n1()
f=False
q=sorted(p)

if p==q:
    f=True
else:
    for i in range(n):
        for j in range(i+1,n):
            a=p.copy()
            a[i],a[j]=a[j],a[i]
            if a==q:
                f=True
                break
p0(f,"YES","NO")

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf


n = iinput()
p = iarr()
a = [i for i in range(1,n+1)]

ans = 'NO'
for i in range(n):
    for j in range(n):
        pp = cp.deepcopy(p)
        pp[i],pp[j] = pp[j],pp[i]
        if pp == a: ans = 'YES'; break
    
print(ans)

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')
        exit()

print('YES')

Solution (Python):

import sys
input = sys.stdin.readline









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

for i in range(N):
    if P[i] != i+1:
        j = P.index(i+1)
        P[i],P[j] = P[j],P[i]
        break

print("YES" if P == list(range(1,N+1)) else "NO")

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

for i in range(n):
    if i+1 != p[i]:
        if switch == 2:
            print("NO")
            exit()
        
        if switch == 1:
            if memory == i+1:
                switch = 2
            else:
                print("NO")
                exit()
            
        else:
            switch = 1
            memory = p[i]

print("YES")

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

list2=[i for i in list1]

    
list1.sort()

count=0

for i in range(n):
    if(list1[i]!=list2[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)
s = sum(pi != qi for pi, qi in zip(p, q))
if s == 0 or s == 2:
    print('YES')
else:
    print('NO')

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

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

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

p=[int(_) for _ 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()))
pi = sorted(p)
c = 0
for i in range(n):
	if p[i] != pi[i]:
		c += 1

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

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

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

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
sp = sorted(p)
ans = []

for i in range(n):
    if p[i] != sp[i]:
        ans.append(p[i])

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b={}
for i in range(len(a)):
    b[a[i]]=i
a.sort()
x=[]
for i in range(n):
    if i!=b[a[i]]:
        x.append([i,b[a[i]]])
if(len(x)>2):
    print("NO")
elif(len(x)==2 and x[0][0]!=x[1][1]):
    print('NO')
else:
    print("YES")

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

l = list()
for i in range(1,N+1):
    l.append(i)
cnt = 0
for i, c in enumerate(p):
    if c != l[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()))
p_sorted = sorted(p)
count = 0
for i in range(n):
    if p[i] != p_sorted[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()))
p2 = sorted(p)
count = 0
for i, j in zip(p, p2):
  if i != j:
    count += 1
    if count > 2:
      print('NO')
      exit()
print('YES')

Solution (Python):
import copy
def or01swap(n, pli):

    plis = copy.copy(pli)
    plis.sort()
    count = 0

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

    return "YES" if count <= 2 else "NO"

def main():
    n = int(input())
    pli = list(map(int, input().split()))
    print(or01swap(n, pli))

if __name__ == '__main__':
    main()

Solution (Python):

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



sorted_P = sorted(P)
change_idx_cnt = 0
for sp, p in zip(sorted_P, P):
    if sp != p:
        change_idx_cnt += 1

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

Solution (Python):
n = int(input())
s = list(map(int,input().split()))
t = 0
for i in range(n):
  s.append(i)

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

Solution (Python):
import copy

n = int(input())
p = list(map(int, input().split()))
p_test = sorted(copy.copy(p))
count = 0

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

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

cnt=0
i=0
while i<N:
    if not List[i]==i+1:
        cnt+=1
    i+=1

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

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

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

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

Solution (Python):
def judge(ps):
    sorted_ps = sorted(ps.copy())
    count = 0
    if ps == sorted_ps:
        return True

    for i,p in enumerate(ps):
        if p != sorted_ps[i]:
            count += 1
        if count > 2:
            return False

    if count == 2:
        return True


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

if judge(ps):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

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

ct = 0
for i in range(n):
    if P[i] != i + 1:
        ct += 1
    if ct > 2:
        print('NO')
        sys.exit()
if ct == 2 or ct == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(readline())



def main():
    N = gete(int) 
    p = list(geta(int))
    q = sorted(p)

    cnt = 0
    for pi, qi in zip(p,q):
        if pi != qi:
            cnt += 1
    
    if cnt <= 2:
        print('YES')
    else:
        print('NO')
	
if __name__ == "__main__":
    main()

Solution (Python):
from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

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

Solution (Python):
n=int(input())
s=[0]*n
s[:]=map(int,input().split())
import copy
s0=copy.copy(s)
s.sort()
a=[1 if s[ii]!=s0[ii] else 0 for ii in range(len(s))]


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

Solution (Python):
def myAnswer(N:int,P:list) -> str:
   flag = False
   for i in range(N):
      if(i+1 != P[i]):
         if(flag):
            return "NO"
         flag = True
         P[P.index(i + 1)] = P[i]
         P[i] = i + 1
   return "YES"

def modelAnswer():
   return
def main():
   N = int(input())
   P = list(map(int,input().split()))
   print(myAnswer(N,P))
if __name__ == '__main__':
   main()

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

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

Solution (Python):
import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
n=int(input())
a=list(map(int,stdin.readline().split()))
c=0
for i in range(1,n+1):
    if(a[i-1]!=i):
        c+=1
if(c>2):
    print("NO")
else:
    print("YES")