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):
def main():
    N = int(input())
    P = list(map(int, input().split()))
    diff = 0
    for p_org, p_sort in zip(P, sorted(P)):
        if p_org != p_sort:
            diff += 1
    if diff == 0 or diff == 2:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()

    cnt = 0
    for n in range(N):
        if P[n] != n + 1:
            cnt += 1
        if cnt > 2:
            print('NO')
            exit()
    else:
        print('YES')


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys, math
from collections import defaultdict
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
P = rl()

import itertools

def check(P):
	
	prev = P[0]
	for i in range(1, len(P)):
		if prev > P[i]:
			return False
		prev = P[i]
	return True

P0 = list(P)
if check(P):
	print('YES')
	exit()
for c in itertools.combinations(list(range(N)), 2):
	i, j = c
	
	P = list(P0)
	P[i], P[j] = P[j], P[i]
	if check(P):
		print('YES')
		exit()
print('NO')

Solution (Python):
N = input()
number_list = [int(i) for i in input().split()]
sorted_list = sorted(number_list)
count = 0
for i,j in zip(number_list, sorted_list):
    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()))
n_l = list(range(1,n + 1))
count = 0
for n_num , p_num in zip(n_l,p):
  if n_num != p_num:
    count += 1
if count == 2 or count == 0:
  print('YES')
else:
  print('NO')

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

Solution (Python):
def resolve():
    N = int(input())
    P = list(map(int, input().split()))
    expected = []
    real = []
    for i in range(1, N+1):
        if i == P[i-1]:
            continue
        expected.append(i)
        real.append(P[i-1])
    print("YES" if len(real) == 0 or (len(real)==2 and len(expected)==2 and expected[0]==real[1] and expected[1]==real[0]) else "NO")

if '__main__' == __name__:
    resolve()

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

Solution (Python):
import numpy as np
n = int(input())
p = np.array(list(map(int,input().split())))

a = np.arange(1, n+1)
tmp = p - a
cnt = np.count_nonzero(tmp != 0)

flg = False
if cnt == 0 or (cnt == 2 and tmp.sum() == 0):
    flg = True

print('YES') if flg else print('NO')

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

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

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

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
li = rl()
li1 = sorted(li)
cnt = 0
for a, b in zip(li, li1):
  if a != b:
    cnt += 1
print('NO' if cnt > 2 else 'YES')

Solution (Python):
import numpy as np
n = int(input())
a = list(map(int, input().split()))
if a == [i for i in range(1, n+1)] or sum(np.array(a)==np.array([i for i in range(1, n+1)]))==n-2:
    print("YES")
else:
    print("NO")

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    p = na()
    for i in range(n-1):
        for j in range(i, n):
            q = p.copy()
            q[i] = p[j]; q[j] = p[i]
            now = q[0]
            f = True
            for qi in q:
                if now > qi:
                    f = False
                    break
                now = qi
            if f:
                print("YES")
                quit()
    print("NO")



if __name__ == "__main__":
    main()

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

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

Solution (Python):
import sys
input = sys.stdin.readline

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

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

cnt = 0
for i in range(N):
  for j in range(i+1,N):
    temp = 0
    good = True
    for k in range(N):
      if k == i:
        if temp > p[j]:
          good = False
          break
        else:
          temp = p[j]
      elif k == j:
        if temp > p[i]:
          good = False
          break
        else:
          temp = p[i]
      else:
        if temp > p[k]:
          good = False
          break
        else:
          temp = p[k]
    if good:
      break
  if good:
    break

temp = 0
good2 = True
for k in range(N):
    if temp > p[k]:
      good2 = False
      break
    else:
      temp = p[k]
    
if good or good2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
pn = [int(num) for num in input().split()]

k = 0
for i in range(0, len(pn)):
    if (i+1) != pn[i]:
      k += 1

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

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

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

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

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

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

for i in range(0,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()))
d = 0

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

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

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

for p,q in zip(P,Q):
  if p!=q:
    d+=1

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

Solution (Python):
n,*p=map(int,open(0).read().split());l=len(p)
np=sorted(p);cou=0
for i in range(l):
  if p[i]!=np[i]:cou+=1
print("YES" if (cou==2 or cou == 0) 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 or cnt==0:print("YES")
else: print("NO")

Solution (Python):
N=input()
N=int(N)
ls=[int(s) for s in input().split()]
count=0
for i in range(N):
  if ls[i]==i+1:
    count=count+1
if count>=N-2:    
  print("YES")
else:
  print("NO")

Solution (Python):
n=int(input())
p=[int(j) for j in input().split()]
s=range(n+1)
x=0
  
c=0

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

if c>2:
  print('NO')
  
if c<=2:
  print('YES')

Solution (Python):
n=[int(s) for s in input().split()]
p=[int(s) for s in input().split()]
count=0
expected=0
a=0
f=0
for i in p:
  if i==a+1:
    a+=1
  else:
    if count>0:
      f=1
    if expected==0:
      expected=i
      a+=1
    else:
      if expected==a+1:
        count+=1
        a+=1
      else:
        f=1

if f==0:
  print('YES')
else:
  print('NO')

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ok=True
for i in range(1,n):
  if p[i]-p[i-1]>0:
    continue
  ok=False
if ok:
  print("YES")
  exit(0)

for i in range(n):
  for j in range(n):
    if i==j:continue
    t=p[:]
    taihi=t[i]
    t[i]=t[j]
    t[j]=taihi
    ok=True
    for k in range(1,n):
      if t[k]-t[k-1]>0:
        continue
      ok=False
    if ok:
      print("YES")
      exit(0)
print("NO")

Solution (Python):
import sys

N = int(sys.stdin.readline())
P = list(map(int, sys.stdin.readline().split()))

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


if not targets:
    print("YES")
elif len(targets) == 2:
    for i, v in targets.items():
        if v-1 in targets and targets[v-1] == i+1:
            print("YES")
        else:
            print("NO")
        break
else:
    print("NO")

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

if __name__ == '__main__':
    solve()

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    p = list(IL())
    q = sorted(p)
    flag = 3
    for i in range(n):
        if p[i] != q[i]:
            flag -= 1
        if not flag:
            print("NO")
            exit()
    print("YES")
    return

if __name__=='__main__':
    Main()

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

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Ps = list(mapint())

S = Ps[:]
S.sort()

cnt = 0
for p, s in zip(Ps, S):
    if not p==s:
        cnt += 1

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

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

if p == l:
    ans = "YES"
cnt = 0
for i in range(n):
    if p[i] != l[i]:
        if p[p[i]-1] == l[i]: cnt += 1
        else:
            ans = "NO"
            break
if cnt > 0:
    print("YES" if cnt//2 == 1 else "NO")
else:
    print(ans)

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

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

if all(a<b for a,b in zip(P,P[1:])):
    print('YES')
    exit()

for i in range(N-1):
    for j in range(i+1, N):
        P[i],P[j] = P[j],P[i]
        if all(a<b for a,b in zip(P,P[1:])):
            print('YES')
            exit()
        P[i],P[j] = P[j],P[i]
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 >= N - 2:
    print("YES")
else:
    print("NO")

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

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()
    cnt = 0
    for n in range(N):
        if P[n] != n + 1:
            cnt += 1
        if P[P[n]-1] != n+1:
            print('NO')
            exit()
    print('NO' if cnt > 3 else 'YES')


if __name__ == '__main__':
    main()

Solution (Python):

n, *p = map(int, open(0).read().split())
print("YNEOS"[sum(p[i] != i+1 for i in range(n)) not in (0, 2)::2])

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

sort_P = list(range(1, N+1))
wrong = 0

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

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

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

Solution (Python):
N = int(input())
Sort = [str(x) for x in range(1,N+1)]
P = [str(x) for x in input().split()]

Count = 0
for T in range(0,N):
    if Sort[T] != P[T]:
        Count += 1
if Count==0 or Count==2:
    print('YES')
else:
    print('NO')

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

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

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

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

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

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

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

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

Solution (Python):
n = int(input())
p = [int(z) for z in input().split()]
k = sorted(p)
poss = False

for i in range(n):
      for j in range(i+1,n):
            new = p[:i]+[p[j]]+p[i+1:j]+[p[i]]+p[j+1:]
            
            if (new)==k:
                  poss = True
                  break
if (p==k):
      poss=True

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

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

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

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

    tmp = 0
    for i in range(len(inlis)):
        if inlis[i] != sortlis[i]:
            tmp += 1
    if tmp == 0 or tmp == 2:
        print("YES")
    else:
        print("NO")

    
if __name__ == "__main__":
    main()

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

newlist=[x-y for (x,y) in zip (p,alis)]


if sum(1 for x in newlist if x!=0)==2 or sum(1 for x in newlist if x!=0)==0 :
    print('YES')
else:
    print('NO')

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

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

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

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

Solution (Python):
from sys import exit
n=int(input())
p=list(map(int,input().split()))
s=sorted(p)
if p==s:
	print("YES")
	exit(0)
for i in range(n-1):
	for j in range(i+1,n):
		p[i],p[j] = p[j],p[i]
		if p == s:
			print("YES")
			exit(0)
		p[i],p[j] = p[j],p[i]
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):
n = int(input())
p = list(map(int,input().split()))
q = p
k = sorted(p)
count = 0
for i in range(n):
    if q[i] != k[i]:
        count += 1
if count == 2 or count ==0:
    print("YES")
else:
    print("NO")

Solution (Python):
length = int(input())
list_p = list(map(int,input().split()))

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

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

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

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

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()
    Q = [n+1 for n in range(N)]

    cnt = 0
    for p, q in zip(P, Q):
        if p != q and P[p-1] != q:
            print('NO')
            exit()
        elif p != q:
            cnt += 1
    else:
        if cnt/2 < 2: print('YES')
        else: print('NO')


if __name__ == '__main__':
    main()

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

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

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

flg = 0
for num, val in enumerate(p):
    if (num + 1) != val:
        flg += 1
if flg == 2 or flg == 0:
    print("YES")
else:
    print("NO")

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

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

arr2 = sorted(arr)

count = 0 

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

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

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *P = map(int, read().split())

    k = sum(1 for i, p in enumerate(P, 1) if i != p)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *P = map(int, read().split())
    
    if P == sorted(P):
        print('YES')
        return
    
    for i in range(N-1):
        for j in range(i+1, N):
            B = P[:]
            B[i] = P[j]
            B[j] = P[i]
            if B == sorted(P):
                print('YES')
                return
                
    print('NO')
    return


if __name__ == '__main__':
    main()

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

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

Solution (Python):


def swap(k, a, b):
    k[a], k[b] = k[b], k[a]
    return k

n = int(input())
l = list(map(int, input().split()))
num = 0
k = []
ans = []

for i in range(len(l)):
    k.append(l[i])

k.sort()

for i in range(n):
    if k[i] != l[i]:
        num += 1
        ans.append(i)

if num == 2 :
    swap(l, ans[0], ans[1])
    if k == l:
        print("YES")
    else:
        print("NO")
elif num == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
flg = False
for i in range(N - 1):
    for j in range(i, N):
        temp_p = p.copy()
        temp = temp_p[i]
        temp_p[i] = temp_p[j]
        temp_p[j] = temp
        if temp_p == sorted_p:
            print('YES')
            flg = True
            break
    if flg:
        break
else:
    print('NO')

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

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

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

if __name__ == '__main__':
    main()

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
l = LI()
x = 0
for i in range(1, N+1):
    if i != l[i-1]:
        x += 1
if x > 2:
    print('NO')
else:
    print('YES')

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

new_list = sorted(P)
cnt=0
for i,e in zip(P,new_list):
  if i != e:
    cnt+=1

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
pf=sorted(p)
cnt=0
for i in range(n):
  if p[i] > pf[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
print("YES" if count<=2 else "NO")

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

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

Solution (Python):
import copy
import sys
N = int(input())
H = list(map(int,input().split()))
C = 0

if sorted(H) == H:
	print("YES")
	sys.exit()

for i in range(0,N-1):
	for j in range(1,N):
		L = copy.copy(H)
		L[i],L[j] = L[j],L[i] 
		if L == sorted(H):
			print("YES")
			sys.exit()
print("NO")

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

LPS = []
LPS = sorted(LP)

cnt = 0
for i in range(N):
    if LP[i] != LPS[i]:
        cnt += 1
        if cnt == 3:
            print("NO")
            exit()

print("YES")

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

ps = sorted(p)

cnt = 0
for i in range(len(p)):
  if p[i] != ps[i] :
    cnt += 1
    if cnt > 2 :
      print("NO")
      exit()

print("YES")

Solution (Python):
def solve():
    n = int(input())
    p = list(map(int, input().split()))
    l = range(1,n+1)
    count = 0
    for i, j in zip(p, l):
        if i != j:
            count += 1
    print('YNEOS'[count>2::2])

if __name__ == '__main__':
    solve()

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

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

Solution (Python):
s = 0
N = int(input())
H = list(map(int,input().split()))
K = list(range(1,N+1))

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

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

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

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

for j in range(N):
    for k in range(N):
        p[j],p[k]=p[k],p[j]
        if p==li:
            print("YES")
            exit()
        else:
            p[j],p[k]=p[k],p[j]

print("NO")

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

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

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

Solution (Python):
n = int(input())
li1 = list(map(int,input().split()))
li2 = sorted(li1)

cnt = 0

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

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