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

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

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

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

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

flag = True
for i in range(n):
    for j in range(i+1,n):
        tmp = p[i]
        p[i] = p[j]
        p[j] = tmp
        if p == sorted(p):
            print('YES')
            flag = False
            tmp = p[i]
            p[i] = p[j]
            p[j] = tmp
            break
        tmp = p[i]
        p[i] = p[j]
        p[j] = tmp

if p == sorted(p):
    print('YES')
    flag = False

if flag:
    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==0 or c==2:
    print("YES")
else:
    print("NO")

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

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

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

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

Solution (Python):





import sys

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

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

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

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

if __name__ == '__main__':
    main()

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

not_same = 0
for i, j in zip(p, range(n)):
    if i != j+1:
        not_same += 1
if not_same < 3:
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

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

l=sorted(a)

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

Solution (Python):


item_count = int(input().rstrip())

numbers = [int(i) for i in input().rstrip().split(" ")]
cnt = 0
for i in range(item_count):
    if numbers[i] != i+1:
        cnt += 1

print("YES" if cnt in {0,2} else "NO")

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

t = sorted(p)
l = 0
for i in range(n):
  if p[i] != t[i]:
    l += 1
    if l > 2:
      print('NO')
      break
else:
  print('YES')

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

Solution (Python):
n = int(input())
plst = list(map(int,input().split()))
plst_sort = sorted(plst)
cnt = 0
for i in range(len(plst)):
    if plst[i] != plst_sort[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()))
q = sorted(p)
cnt = 0
for i in range(N-1):
  if p[i] != q[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)

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
p = [int(x) for x in input().split()]

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

if miss <= 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(1,n+1):
    p_i=p.index(i)
    if p_i!=i-1:
        if p[i-1]==p_i+1 and count<=2:
            count+=1
        else:
            ans="NO"
            break
print(ans)

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

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

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

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 = [int(a) for a in input().split()]
print("YES" if sum([1 for i in range(N) if A[i] != i+1]) <= 2 else "NO")

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

P=sorted(p)
diff=[]
index=[]



if P==p:
    print("YES")
else:
    for i in range(N):
        if p[i]!=P[i]:
            diff.append(p[i])
            index.append(i)
    if len(diff)>2:
        print("NO")
    else:
        p[index[0]]=diff[1]
        p[index[1]]=diff[0]
        if P==p:
            print("YES")
        else:
            print("NO")

Solution (Python):
N = int(input())
ps = list(map(int, input().split()))
diffs = []
for i, p in enumerate(ps):
    index = i + 1
    if index != p:
        diffs.append((index, p))

diffs_size = len(diffs)
if diffs_size == 0:
    print("YES")
elif diffs_size == 2:
    a, b = diffs[0], diffs[1]
    if a[0] == b[1] and a[1] == b[0]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

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

p_s = sorted(p)
c = 0
for i in range(n):
    if p[i] != p_s[i]:
        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("YES")
else:
  print("NO")

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

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

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

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
temp = 0
i = 1
while i <= n:
    if i != p[i-1]:
        temp += 1
    i += 1
print("YES" if temp < 3 else "NO")

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

Solution (Python):
import sys

n, *p = map(int, sys.stdin.read().split())


def main():
    cnt = 0
    for i in range(n):
        if p[i] != i + 1:
            cnt += 1
    
    if not cnt or cnt == 2:
        return 'YES'
    return 'NO'

if __name__ == '__main__':
    ans = main()
    print(ans)

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

p_sort = sorted(p)
key = [x-y for x,y in zip(p,p_sort)]
cnt = 0
for w in key:
    if w != 0:
        cnt += 1
        if cnt > 2:
            break
if cnt > 2:
  print('NO')
else:
    print('YES')

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


n = int(input())
p = readints()

q = sorted(p)

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

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

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

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

Solution (Python):
import copy
N=int(input())
p=list(map(int,input().split()))
s=sorted(p)
if s==p:
  print('YES')
else:
  flag=0
  tmp=[]
  for i in range(N):
    for j in range(i+1,N):
      tmp=copy.deepcopy(p)
      tmp[i],tmp[j]=tmp[j],tmp[i]
      if tmp == s:
        flag=1
  print('NYOE S'[flag::2])

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

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

Solution (Python):



n = int(input())
p = [int(x) for x in input().split()]
q = sorted(p)
r = [p1 != q1 for p1, q1 in zip(p, q)]
print("YES" if r.count(True) == 2 or r.count(True) == 0 else "NO")

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

for i, j in zip(p, s):
    if i != j:
        count += 1

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

Solution (Python):
def swap_ij(xs, i, j):
    xs[i], xs[j] = xs[j], xs[i]


def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    sorted_p = sorted(p)

    for i in range(N):
        for j in range(i, N):
            swap_ij(p, i, j)
            if p == sorted_p:
                print("YES")
                return
            else:
                swap_ij(p, i, j)

    print("NO")


if __name__ == "__main__":
    main()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
p = LIST()

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

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

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

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

success = True
change = 0

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

  else:
    if P[P[i]-1] == i+1:
      change  += 1
    else:
      success = False
      break

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

Solution (Python):
input()
p = list(map(int, input().split()))
a = 0
for i, p_ in enumerate(p):
  a += (p_ != (i + 1))
print('YNEOS'[a > 2 :: 2])

Solution (Python):
N = int(input())
p = tuple(map(int, input().split()))
print('YES' if sum(i != j for i, j in zip(p, range(1, N+1))) <= 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
print('YES' if cnt <= 2 else 'NO')

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

Solution (Python):



import math


def main():
    N = int(input())
    P = list(map(int, input().split()))
    tmp = [f != s for f, s in zip(P, sorted(P))]
    if sum(tmp) <= 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 in range(n):
    if i+1!=p[i]: cnt+=1
        
print('YES' if cnt<=2 else 'NO')

Solution (Python):
n = int(input())
s = list(map(int, input().split()))
ss = list(i for i in range(1, n + 1))
cnt = 0
flag = False
for i in range(n):
    if s[i] == ss[i]:
        cnt += 1
    if cnt >= n - 2:
        flag = True
    else:
        flag = False

print("YES") if flag else print("NO")

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

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

count = 0
for n, p in zip(range(1, N+1), P):
    if n != p:
        if count == 0:
            count += 1
            i = P.index(p)
            j = P.index(n)
            P[j] = P[i]
            P[i] = n
        else:
            print('NO')
            break
else:
    print('YES')

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

count = 0
for i, j in zip(p, pp):
    if i != j:
        count += 1
    if count == 3: break

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

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

Solution (Python):
import heapq
N = int(input())
P = list(int(x) for x in input().split())
list = P.copy()
heapq.heapify(list)
count = 0

for i in range(N):
    if P[i] != heapq.heappop(list):
        count += 1

    if count == 3:
        break

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

Solution (Python):


n = input()
a = [int(x) for x in input().split()]

b = sorted(a)

total = 0
for x, y in zip(a, b):
    if x != y:
        total += 1

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

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

diff = 0
for ind, i in enumerate(p):
    if ind+1!=i:
        diff+=1

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

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

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

v = sum(1 for i,j in zip(range(1, N+1), p) if i!=j)
print("YES" if v<=2 else "NO")

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

flag=0

for i in range(0,N-1):
    if i+1!=P[i]:
        for j in range(i+1,N):
            if j+1!=P[j]:
                if j!=N-1:
                    for k in range(j+1,N):
                        if k+1!=P[k]:
                            flag=1
                        break
if flag==0:
    print('YES')
else:
    print('NO')

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

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

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

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

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

count = 0
for n in range(N):
    if P[n] != n+1:
        count += 1
    if count > 2:
        break

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

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

count = 0
for n in range(N):
    p_dif = P[n] - P_sorted[n]
    if p_dif != 0:
        count += 1
    if count > 2:
        break

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

Solution (Python):
N = int(input())
list = list(map(int, input().split()))
list_sort = sorted(list)
count = 0

for a in range(N):
    if list[a] != list_sort[a]:
        count += 1


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

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

b=sorted(a)

c=0
for i in range(N):
  if (a[i])!=(b[i]):
    c+=1
    if c>=3:
      print('NO')
      sys.exit()
print('YES')

Solution (Python):
def solve(n, A):
    def check(L):
        for i in range(1, len(L)):
            if L[i] < L[i - 1]:
                return False
        return True
    if check(A):
        return 'YES'
    for i in range(n):
        for j in range(i + 1, n):
            if check(A[0:i] + [A[j]] + A[i + 1:j] + [A[i]] + A[j + 1:]):
                return 'YES'
    return 'NO'

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

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

Solution (Python):
n = int(input())
pl = list(map(int, input().split()))
pl_sort = sorted(pl)

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

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

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
r=[i+1 for i in range(N)]
ans=False
for i in range(0,N-1):
    for j in range(i+1,N):
        p[i],p[j]=p[j],p[i]
        if p==r:
            ans=True
        
        p[i], p[j] = p[j], p[i]
if p==r:
    ans = True
print('YES' if ans else 'NO')

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


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

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

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

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

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

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

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

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

q = sorted(p)
ans = 0
for _p, _q in zip(p, q):
    if _p != _q:
        ans += 1

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

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

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

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

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

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  


def solve(N: int, p: "List[int]"):
    sorted_p = sorted(p)
    ans = [i!=j for i,j in zip(p,sorted_p)]
    if sum(ans) <= 2:
        print(YES)
    else:
        print(NO)
    return



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

if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

Solution (Python):
a=int(input())
l=list(map(int,input().split()))
t=[]
b=[]
for x in range(1,a+1):
    t.append(x)
for x in range(a):
    b.append( l[x]==t[x])
if b.count(True)==a:print("YES")
elif b.count(False)!=2:print("NO")
else:
    fi=b.index(False)
    li=b.index(False,fi+1)
    tmp=l[fi]
    l[fi]=l[li]
    l[li]=tmp
    b.clear()
    for x in range(a):
        b.append( l[x]==t[x])
    if b.count(True)==a:print("YES")
    else:print("NO")

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

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

P=sorted(A)
cnt=0

for i in range(n):
    if A[i] != P[i]:
        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(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()))

P=sorted(A)
cnt=0

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

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

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

Solution (Python):



import sys
input = sys.stdin.readline


def solve(N, P):
    PP = sorted(P)
    cnt = 0
    for l1, l2 in zip(P, PP):
        if l1 != l2:
            cnt += 1

    return 'YES' if cnt <= 2 else 'NO'


N = int(input())


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








print(solve(N, P))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
ll=sorted(l)
cnt=0
for l1,l2 in zip(l,ll):
  if l1 != l2:
    cnt+=1
    
print("YES" if cnt <= 2 else "NO")

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

p_sorted = sorted(p)

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

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

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

sorted_p = sorted(p_i)
count = 0

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

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

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

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

q = [int(y) for y in range(1, N+1)]

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())
A=list(map(int,input().split()))
count=int(0)

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

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

Solution (Python):

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

srt = sorted(a)

res = "YES"
cnt = 0
for c,s in zip(a,srt):
    if c != s:
        cnt += 1 
    if cnt > 2:
        res = "NO"
        break
print(res)

Solution (Python):

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

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

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

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
p_asc = [i for i in range(1, n + 1)]
p_diff = [pi - p_asci for pi, p_asci in zip(p, p_asc)]
if (p_diff.count(0) == n) or (p_diff.count(0) == n - 2):
    print('YES')
else:
    print('NO')

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

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

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

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

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

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

newlist = sorted(list)

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

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

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

newlist = sorted(list)

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

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

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

lstI = list(map(int, input().split()))
sortedList = sorted(lstI)

count = 0
for i in range(0, len(lstI)):
    if lstI[i] != sortedList[i]:
        count += 1

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

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

l=list(range(1,n+1))
cnt=0

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

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

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

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

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

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

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

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

if n == p:
	print("YES")
	exit()

count = 0
for i, j in zip(n,p):
	if i != j:
		count += 1

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

Solution (Python):
n = int(input())
*p, = 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):
N=int(input())
p=list(map(int,input().split()))
buf=0
ans=p.copy()
count=0
list2=[]

ans.sort()


flag=0

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

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

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

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

O = list(range(1, n + 1))
P = list(map(int, input().split()))

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

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

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

Solution (Python):
N=int(input())
P = [int(e) for e in input().split()]
cnt=0
for i in range(N):
  if P[i]!=(i+1):
    cnt+=1
if (cnt==0) or (cnt==2):
  print('YES')
else:
  print('NO')

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

for i in range(a):
  if li[i] != i+1:
    count += 1

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

Solution (Python):
def LI():
  return [int(s) for s in input().split()]

N = int(input())
p_s = LI()
p_s_sort = sorted(p_s)

diff_count = 0

for i in range(N):
  if p_s[i] != p_s_sort[i]:
     diff_count += 1
    
if diff_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
    else:
        cnt += 0
if cnt >= N-2:
    print("YES")
else:
    print("NO")

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