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 copy
n = int(input())
li = list(map(int,input().split()))
li1 = copy.copy(li)
li1.sort()
cnt = 0
for i in range(n):
    if li[i] != li1[i]:
        cnt += 1

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

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

if p == sorted(p):
    print('YES')
else:
    ret = 'NO'
    
    for i in range(N-1):
        for j in range(i+1, N):

            
            if p[i] < p[j]:
                is_sorted = False
                continue
            else:
                k = p.copy()
                k[i], k[j] = k[j], k[i]

            is_sorted = True
            for l in range(N-1):
                if k[l+1] < k[l]:
                    is_sorted = False
                    break

            if is_sorted:
                ret = 'YES'
                break
        if is_sorted:
            break

    print(ret)

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

if p == sorted(p):
    print('YES')
else:
    ret = 'NO'
    is_sorted = False
    
    for i in range(N-1):
        for j in range(i+1, N):
            
            if p[i] < p[j]:
                continue
            else:
                k = p.copy()
                k[i], k[j] = k[j], k[i]






            if k == sorted(p):
                ret = 'YES'
                is_sorted = True
                break
        if is_sorted:
            break

    print(ret)

Solution (Python):

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


result = 'NO'
sorted_p = sorted(p)












count = 0
for i in range(N):
  if p[i] != sorted_p[i]:
    count += 1
if count <= 2:
  result = 'YES'


print(result)

Solution (Python):
N = int(input())
P = tuple(map(int, input().split()))
assert len(P) == N

sortedP = sorted(P)

count = len([p for p,q in zip(P, sortedP) if p != q])
if count == 2 or count == 0:
  print('YES')
else:
  print('NO')

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

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

if len(ido)==2 or len(ido)==0:
	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 += 1
if c in [0, 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 <= 2:
  print("YES")
else:
  print("NO")

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

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

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

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]
r = sorted(p)
for i in range(N):
    for j in range(i+1,N):
        q = []
        for k in range(N):
            if(k == i):
                q.append(p[j])
            elif(k == j):
                q.append(p[i])
            else:
                q.append(p[k])
        if(p == r or q == r):
            print("YES")
            exit()
print("NO")

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
p = l()

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

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

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

p_sorted = sorted(p)
cnt = 0
for i in range(N):
    if p[i] != p_sorted[i]:
        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 == 0 or count == 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N=int(input())
P=[0]+list(map(int,input().split()))

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

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

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

res = False
for i in range(N):
    for j in range(i, N):
        flg = True
        p[i], p[j] = p[j], p[i]
        for k in range(N - 1):
            if p[k] > p[k + 1]:
                flg = False
        if flg:
            res = True
            break
        p[i], p[j] = p[j], p[i]

print(['NO', 'YES'][res])

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

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

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

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

Solution (Python):
def resolve():
    n = int(input())
    p = map(int,input().split())
    cnt = 0
    for i,j in enumerate(p):
        if (i+1) != j:
            cnt += 1
    print('YES' if cnt < 3 else 'NO')
resolve()

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

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

A = copy.copy(p)
A.sort()
cnt = 0

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

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

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

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

Solution (Python):
import copy
N = int(input())
p = list(map(int, input().split()))
pcopy = copy.deepcopy(p)
ans = sorted(p)

is_ascend = False
if p == ans:
    is_ascend = True

for i in range(N):
    for j in range(i+1,N):
        p = copy.copy(pcopy)
        p[i], p[j] = p[j], p[i]
        if p == ans:
            is_ascend = True

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

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

s_p = sorted(p)

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

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

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

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

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

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

Solution (Python):
import math
import collections
import itertools
import copy

def YesNo(Bool):
    if(Bool):
        print("YES")
    else:
        print("NO")
    return

def resolve():

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

    tmp=copy.deepcopy(p)
    isOK=True
    for k in range(N-1):
        if(tmp[k]>=tmp[k+1]):
            isOK=False
    if(isOK):
        ans=True

    for i in range(N):
        for j in range(i+1,N):
            tmp=[]
            tmp=copy.deepcopy(p)
            tmp[i],tmp[j]=tmp[j],tmp[i]
            isOK=True
            for k in range(N-1):
                if(tmp[k]>=tmp[k+1]):
                    isOK=False
            if(isOK):
                ans=True
    YesNo(ans)

resolve()

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

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

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

count = 0
for i in range(N):
    if p[i] != i+1:
        count = count + 1
    if count == 3:
        break
if count == 3:
    print('NO')
else:
    print('YES')

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

q = p.copy()
q.sort()

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

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

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

not_equal = 0
for p, q in zip(nums, sorted_nums):
    if p != q:
        not_equal += 1

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

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

sor_N = sorted(p)

cn = 0

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


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

Solution (Python):
N = int(input())
P = [int(s) for s in input().split()]
index = []
for i in range(N):
    index.append(P.index(i+1))
count = 0
for i, j in enumerate(index):
    if i != j:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
def inpl(): return list(map(int, input().split()))

N = int(input())
P = inpl()
Q = sorted(P)

for i in range(N):
    for j in range(i, N):
        R = [p for p in P]
        R[i], R[j] = R[j], R[i]
        for k in range(N):
            if R[k] != Q[k]:
                break
        else:
            print("YES")
            exit()
else:
    print("NO")

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    P = [int(p) for p in input().split()]
    diff = 0
    for i in range(N):
        if P[i] != i + 1: diff += 1
    if diff <= 2: print("YES")
    else: print("NO")

    return 0

if __name__ == "__main__":
    solve()

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

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

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

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

def swap(a, b):
    return b, a

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = swap(a[i], a[j])
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = swap(a[i], a[j])
print('YES' if ok else 'NO')

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

def swap(a, b):
    return b, a

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = swap(a[i], a[j])
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = swap(a[i], a[j])
print('YES' if ok else 'NO')

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

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = a[j], a[i]
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = a[j], a[i]
print('YES' if ok else 'NO')

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

Solution (Python):
n = int(input())
C = list(i for i in range(1,n+1))
L = list(map(int,input().split()))
cnt = 0
for c,l in zip(C,L):
  if c != l:
    cnt += 1
print('YES' if cnt == 0 or cnt == 2 else 'NO')

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

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

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

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

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

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

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







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

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

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

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

Solution (Python):
def check(n, list1): 
    new = [] 
    for i in range(n): 
        new.append(list1[i]) 
          
    new.sort()
    swaps = 0
    for i in range(n): 
        if list1[i] != new[i]: 
            swaps += 1
              
    if swaps == 0 or swaps == 2: 
        return True
    else: 
        return False
  
n = int(input())

numbers =list(map(int, input().split()[:n]))



if check(n, numbers): 
    print("YES")
else:
    print("NO")

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

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

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

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

i=0
while i<len(List)-1:
    j=i+1
    ListTest=list(List)
    count=0
    while j<len(List):
        if ListTest==List_sorted:
            print('YES')
            quit()
        ListTest[i],ListTest[j]=ListTest[j],ListTest[i]
        if ListTest==List_sorted:
            print('YES')
            quit()
        else:
            ListTest=list(List)
        j+=1
    i+=1
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):
import numpy as np
 
N = int(input())
p = list(map(int, input().split()))
 
q = sorted(p)
p, q = np.array(p), np.array(q)

if np.sum(p != q) <= 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("YES")
else:
  print("NO")

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
cnt=0
for i in range(n):
    if a[i]!=i+1:
        cnt+=1
if cnt>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 i+1 != P[i]:
        cnt += 1
    if cnt > 2:
        print('NO')
        exit()

print('YES')

Solution (Python):


a=int(input())





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


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

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

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

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

Solution (Python):
n = int(input())
plist = list(map(int, input().split()))
nlist = sorted(plist)

ng_cnt = 0
for p, n in zip(plist, nlist):
    if p != n:
        ng_cnt += 1
if ng_cnt >2:
    print('NO')
else:
    print('YES')

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

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

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

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

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

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

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
R = sorted(P)
import copy
answer = 'NO'
for i in range(0,N-1):
    for j in range(i+1,N):
        Q = copy.deepcopy(P)
        A = Q[i]
        B = Q[j]
        Q[i] = B
        Q[j] = A
        if Q == R:
            answer = 'YES'
            break
if P == R:
    answer = 'YES'
print(answer)

Solution (Python):
a = int(input())
b= list(map(int,input().split()))
c = sorted(b)
d = 0
for i in range(a):
  if b[i] != c[i]:
    d += 1
if d <=2:
  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(len(P)):
    if P[i] != sorted_P[i]:
        cnt += 1

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

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

Solution (Python):
N,*P=map(int,open(0).read().split())
S='YES' if sum([P[i]==i+1 for i in range(N)])>=N-2 else 'NO'
print(S)

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 bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    p = LI()

    cnt = len([j for i, j in zip(p, sorted(p)) if i != j])
    if cnt <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    resolve()

Solution (Python):
N=int(input())
ans=0
d=[int(s) for s in input().split()]
for i in range(N):
    if d[i]!=i+1:
        ans+=1
if ans>=3:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

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


if __name__ == '__main__':
    main()

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

np_list=sorted(p_list)

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

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

print(ans)

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

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

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

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

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

Solution (Python):


num = int(input())
str = input().split()
table = list(str)
table5 = [int(i) for i in table]
count = 0
table2 = sorted(table5)
table3 = [int(i) for i in table2]
for i in range(num):
    if table5[i] == table3[i]:
        continue
    else:
        count += 1

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

Solution (Python):
import sys
import itertools
tokens = itertools.chain.from_iterable(map(str.split, sys.stdin))

yesno = ['NO', 'YES']
n = int(next(tokens))
p = [int(next(tokens)) for _ in range(n)]


s = sum(1 if a != b else 0 for a, b in zip(p, sorted(p)))
print(yesno[s == 2 or s == 0])

Solution (Python):
import sys
n=int(input())
p=list(map(int,input().split()))
sort_p=sorted(p)
q=p
if p==sort_p:
  print("YES")
  sys.exit()
for i in range(1,n):
  for j in range(i):
    p[i],p[j]=p[j],p[i]
    if p==sort_p:
      print("YES")
      sys.exit()
    else:
      p[i],p[j]=p[j],p[i]
print("NO")

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

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

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

Solution (Python):






def main():
  N = int(input())
  p = list(map(int,input().split()))
  def solve(n,p):
    assert n >= 2
    target = sum(0 if p[i]==i+1 else 1 for i in range(n)) 
    return target == 0 or target == 2
  print("YES" if solve(N,p) else "NO")
  return

if __name__ == '__main__':
  main()

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

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

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

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

    if cnt == 0 or cnt == 2:
        ans = "YES"
    else:
        ans = "NO"

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
P_sort = sorted(P)
counter = 0
for p,p_sort in zip(P,P_sort):
    if p != p_sort:
        counter += 1
if counter == 2 or counter == 0:
    print('YES')
else:
    print('NO')

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

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

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

memo = 0
mode = 1
for k in range(n):
	if memo > p[k]:
		mode = 0
		break
	memo = p[k]
if mode:
	print("YES")
	exit()

for i,j in itertools.combinations(range(n), 2):
	memo = 0
	mode = 1
	for k in range(n):
		if k == i:
			k = j
		elif k == j:
			k = i
		if memo > p[k]:
			mode = 0
			break
		memo = p[k]
	if mode:
		print("YES")
		exit()
print("NO")

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

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

Solution (Python):
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n = int(input())
    x = list(range(1, n+1))
    p = Input()
    data = [i != j for i, j in zip(x, p)]
    if sum(data) > 2:
        print("NO")
    else:
        print("YES")

main()

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

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

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

p_ = list(range(1, N+1))
p = [int(n) for n in input().split()]

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

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

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

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

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

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

Solution (Python):
from itertools import combinations

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

combi = combinations([i for i in range(n)], 2)
sort = sorted(p)

if sort == p:
        print('YES')
        exit()

for i, j in combi:
    p[i], p[j] = p[j], p[i]
    if sort == p:
        print('YES')
        exit()
    p[j], p[i] = p[i], p[j]
print('NO')

Solution (Python):
import numpy as np

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

before = np.array(P)
after = np.sort(before)
sort_cnt = np.count_nonzero(before - after)

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

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

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

cnt = 0

for i in range(N):
    if lis[i] == i+1:
        cnt += 1


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

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

isPossible = True
for k in range(N-1):
    if P[k] > P[k+1]:
        isPossible = False
if isPossible:
    print("YES")
    exit()


for i in range(N):
    for j in range(i+1, N):
        newList = P.copy()
        newList[i], newList[j] = newList[j], newList[i]
        isPossible = True
        for k in range(N-1):
            if newList[k] > newList[k+1]:
                isPossible = False

        if isPossible:
            print("YES")
            exit()
print("NO")

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

ans = 0

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

print('NO' if ans > 2 else 'YES')

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

Solution (Python):
from itertools import combinations

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

ans = 'NO'
p_sorted = list(sorted(p))
if p == p_sorted:
    ans = 'YES'
else:
    l = [i for i in range(N)]
    c = combinations(l, 2)
    for v in c:
        tmp = p.copy()
        tmp[v[0]], tmp[v[1]] = tmp[v[1]], tmp[v[0]]
        if tmp == p_sorted:
            ans = 'YES'
            break
print(ans)