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

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

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

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

    cnt = 0
    for p, sp in zip(list(ps), sorted_ps):
        if p != sp:
            cnt += 1
            if cnt >= 3:
                print('NO')
                return
    print('YES')


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

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
la = list(range(1,N+1))
cnt1 = 0
cnt2 = 0
while cnt1 <= N-1:
    if P[cnt1] == la[cnt1]:
        cnt2 += 1
    cnt1 += 1
if cnt2 >= N-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("YES")
else:
    print("NO")

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

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

Solution (Python):
input()
l=list(map(int,input().split()))
print('YES' if sum(i!=j for i,j in zip(l,sorted(l)))<3 else 'NO')

Solution (Python):
input()
l = list(map(int, input().split()))
print('YES' if sum(i!=j for i, j in zip(l, sorted(l))) < 3 else 'NO')

Solution (Python):
input()
l = list(map(int, input().split()))
print('YES' if sum(i!=j for i, j in zip(l, sorted(l))) in (0, 2) else 'NO')

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

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

    count = 0

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

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

if __name__ == '__main__':
    main()

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

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

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


for i in range(N):
    if not A_n[i] == i+1:
        
        j = A_n[i]-1
        A_n[i], A_n[j] = A_n[j], A_n[i]
        break


for i in range(N):
    if not A_n[i] == i+1:
        print("NO")
        exit()
print("YES")

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

def solve():
  
  p_sorted = sorted(p, reverse=False)
  cnt = 0
  for i in range(0, n):
    if p[i] != p_sorted[i]:
      cnt += 1

    if cnt >= 3:
      print("NO")
      return

  print("YES")
  return


if __name__ == "__main__":
  solve()

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

pp = sorted(p)
diff = [0 if p[i] == pp[i] else 1 for i in range(n)]
if (sum(diff) == 0 or sum(diff) == 2):
   print('YES')
else:
   print('NO')

Solution (Python):
def main():
    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 >= 3:
        print("NO")
    else:
        print("YES")


if __name__=="__main__":
    main()

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

memo = []
for i, p in enumerate(P):
    if p != Q[i]:
        memo.append(p)

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

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())
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):
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
count = 0

for a, b in zip(p, sorted_p):
    if a != b:
        count += 1
    if count > 2:
        break

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

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

count = 0
for p, s in zip(P, sorted(P)):
  if p != s:
    count += 1

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

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

a = range(1, n+1)
count = n

for i in range(n):
  if p[i]==a[i]:
    n -= 1

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

Solution (Python):
def main():
    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:
            break
    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()))

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

Solution (Python):
"""
author : halo2halo
date : 9, Jan, 2020
"""

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

S = sum(x != y for x, y in enumerate(P, 1))
print('YES' if S <= 2 else 'NO')

Solution (Python):
"""
author : halo2halo
date : 9, Jan, 2020
"""

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, *P = map(int, read().split())
P_N = sorted(P)



S = sum(x != y for x, y in zip(P, P_N))

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

Solution (Python):
n=int(input())
p=[int(x) for x in input().split()]
pp=[int(x+1) for x in range(n)]
 
for i in range(n):
    buf=i+1
    if buf != p[i]:
        p[p.index(buf)]=p[i]
        p[i]=buf
        break
 
if pp==p:
    print("YES")
else:
    print("NO")

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

count=0

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

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

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

l = []
for k in range(1, N+1):
    l.append(k)

count = 0

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

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

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

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

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

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

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

m=sorted(l)

e=0

for i in range(n-1):
    if l[i]!=m[i]:
        e+=1
if e<=2:
    print('YES')
else:
    print('NO')

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

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

    ok = 1
    flg = 1
    pair = -1
    for i in range(N):
        if P[i] != i+1:
            if flg:
                if P[P[i]-1] == i+1:
                    flg = 0
                    pair = P[i]-1
                else:
                    ok = 0
                    break
            else:
                if i == pair:
                    continue
                ok = 0
                break

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


if __name__ == '__main__':
    main()

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

dif = sum(idx != pp for idx, pp in enumerate(p, 1))  
if dif <= 2:
    print("YES")
else:
    print("NO")

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

dif = 0
for idx, pp in enumerate(p, 1):  
    dif += (idx != pp)
if dif <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import numpy as np

n = int(input())
p = np.array([int(_) for _ in input().split()])
pp = sorted(p)
dif = pp - p
non_zero = len(p)-np.count_nonzero(dif==0)
if (non_zero == 2) or (non_zero == 0):
    print("YES")
else:
    print("NO")

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
q = sorted(p)

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

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

p_sort = sorted(p)

ans = 0

for i in range(len(p)):
  if p[i] != p_sort[i]:
    ans += 1

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

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

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

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

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

cnt = 0
for i in range(len(P)):
    if i+1 != P[i]:
        cnt += 1
        if cnt == 3:
            print('NO')
            exit()
print('YES')

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


if __name__ == '__main__':
    main()

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

for i in range(n):
    p[i]-=1
cnt=0
ans="YES"
for i,p_i in enumerate(p):
    if i==p_i:
        continue
    cnt+=1
    if cnt>1:
        ans="NO"
        break
    for j,p_j in enumerate(p):
        if i==p_j:
            p[j]=p_i
            p[i]=p_j
            break

print(ans)

Solution (Python):
def main():
  N = int(input())
  ps = [i for i in map(int, input().split())]
  cou = 0

  for i in range(N):
    if ((i+1) != ps[i]) and cou==0:
      tp = i+1
      cou += 1
    elif ((i+1) != ps[i]) and cou==1:
      if ps[i] != tp:
        return('NO')
      cou += 1
    elif ((i+1) != ps[i]) and cou==2:
      return('NO')
  return('YES')
if __name__ == '__main__':
  print(main())

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

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

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

ans = 0
for a, b in zip(ps, sorted(ps)):
    if a != b:
        ans += 1

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

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]
arr = list(range(1, N + 1))
diff = 0
for i in range(N):
    if P[i] != arr[i]:
        diff += 1
if diff >= 3:
    print("NO")
else:
    print("YES")

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

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

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

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

for i, p in enumerate([int(_) for _ in input().split()], 1):
    cnt += i != p
print('YES' if cnt <= 2 else 'NO')

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

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

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

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

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

    diff = 0
    for i in range(N):
        if (i+1) != p[i]:
            diff += 1
    if (diff == 2) | (diff == 0):
        return "YES"
    return "NO"
if __name__ == '__main__':
    print(main())

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

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

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

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

Solution (Python):
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(range(1, n+1)))
if sum(a != b) <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
A = int(input())
B = list(map(int,input().split(" ")))
C = sorted(B)
S = 0
for i in range(A):
    if B[i] != C[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()))

p_sorted = sorted(p)

ans = 0

for i in range(n):
  if p_sorted[i] != p[i]:
    ans += 1
  else:
    pass

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

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


	
 
 
if __name__ == '__main__':
	main()

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

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

Solution (Python):



import numpy as np


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


ideal = np.sort(p)

if sum(p != ideal) <= 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 i+1 != l[i]:
        count += 1

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

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

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

Solution (Python):
def main(li):
    count = 0
    for i in range(len(li)):
        if li[i] != i + 1:
            count += 1
    if count in [0, 2]:
        return True
    else:
        False


if __name__ == '__main__':
    n = int(input())
    li = list(map(int, input().split()))
    if main(li):
        print('YES')
    else:
        print('NO')

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

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

Solution (Python):
N = int(input())
li = list(map(int, input().split()))
ans = list()
for i in range(N):
  if i+1 != li[i]:
    ans.append('groot')
if len(ans) <= 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):
    
    if P[i] != i + 1:
        cnt += 1
if cnt > 2:
    print('NO')
else:
    print('YES')

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

SortedP = P.copy()
SortedP.sort()
count=0
for i in range(N):
    if(P[i]!=SortedP[i]):
        count = count + 1
    if(count>2):
        print("NO")
        break
else:print("YES")

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

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

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

    if c > 2:
        print("NO")
        break
else:
    print("YES")

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

for i in range(len(p) - 1):
    for j in range(i + 1, len(p)):
        if p[i] > p[j]:
            tmp = min(tmp, p[j])

    if tmp != 100:
        for k in range(len(p)):
            if p[k] == tmp:
                p[k], p[i] = p[i], p[k]
                break
        break

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

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

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

cnt = 0

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

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

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = sorted(A)
c = 0
for i, j in zip(A, B):
    if (i !=j):
        c += 1
if c < 3:
    print("YES")
else:
    print("NO")

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

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

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

for i in range(n):
    if p[i] == sorted(p)[i]:
        tmp += 1

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

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

lst = []
for i in range(n):
    lst.append(i+1)

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

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

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

swap = 0
for i, p in enumerate(P):
    if p != i + 1:
        swap += 1

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

Solution (Python):
_ = input()
P = [int(i) for i in input().split()]
P_sorted = sorted(P)
swap = 0
for p, s in zip(P, P_sorted):
    if p != s:
        swap += 1

ans = "YES" if swap <= 2 else "NO"

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

    c = 0
    for i, v in enumerate(p):
        if (i + 1) != v:
            c += 1
        if c > 2:
            print('NO')
            quit()
    print('YES')

if __name__ == '__main__':
    main()

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

p_sorted = sorted(p)
count = 0

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

Solution (Python):
import copy

def is_increase(arr):
    for i in range(1, len(arr)):
        if arr[i] < arr[i-1]:
            return False
    return True

N = int(input())
p = list(map(int, input().split()))
for i in range(N-1):
    for j in range(i, N):
        p2 = copy.deepcopy(p)
        p2[i], p2[j] = p2[j], p2[i]
        if is_increase(p2):
            print('YES')
            exit(0)
print('NO')

Solution (Python):
N, *L,  = [list(map(int, s.split())) for s in open(0)]
A = L[0]

A_sorted = sorted(A)
flag = 0
if A == A_sorted:
    flag += 1
for i in range(len(A) - 1):
    for j in range(i+1, len(A)):
        A_ = A.copy()
        tmp = A_[i]
        A_[i] = A_[j]
        A_[j] = tmp
        if A_sorted == A_:
            flag += 1
if flag:
    print("YES")
else:
    print("NO")

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

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

if __name__ == '__main__':
    main()

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

if p == sorted(p):
    print("YES")
    exit()
else:
    for i in range(n):
        for j in range(n):
            if i != j:
                q = p.copy()
                q[i], q[j] = q[j], q[i] 
                if q == sorted(q):
                    print("YES")
                    exit()

print("NO")

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

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


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

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

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

for i, num in enumerate(a):
  if i+1 != num:
    count += 1

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

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

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

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

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

Solution (Python):
n=int(input())
lst=list(map(int,input().split()))
cnt=0
for i in range(n):
  if lst[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)
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 = [x for x in map(int, input().split())]

memo = []

for i in range(1, n):
    if p[i - 1] > p[i]:
        memo.append(i)

flag = True

if len(memo) >= 3:
    flag = False
elif len(memo) == 0:
    flag = True
elif len(memo) == 1:
    pp = p
    pp[memo[0] - 1], pp[memo[0]] = pp[memo[0]], pp[memo[0] - 1]
    for i in range(1, n):
        if pp[i - 1] > pp[i]:
            flag = False
            break
elif len(memo) == 2:
    pp = p
    pp[memo[0] - 1], pp[memo[1]] = pp[memo[1]], pp[memo[0] - 1]
    for i in range(1, n):
        if pp[i - 1] > pp[i]:
            flag = False
            break

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

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

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

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

Solution (Python):
import sys

def fi(li):
    for i in range(1,len(li)):
        if li[i-1] > li[i]:
            return False
    return True

def main():
    n=int(input())
    ps=list(map(int,input().split()))
    if fi(ps):
        print("YES")
        return 0
    for i in range(len(ps)):
        for j in range(i,len(ps)):
            if ps[i]>ps[j]:
                ps[i],ps[j]=ps[j],ps[i]
                if fi(ps):
                    print("YES")
                    return 0
                ps[i],ps[j]=ps[j],ps[i]
    print("NO")
    return 0

if __name__ == "__main__":
    sys.exit(main())

Solution (Python):
import copy
n = int(input())
l = list(map(int, input().split()))
m = sorted(copy.copy(l))
check = [True] * n
for i in range(n):
    if not (l[i] == m[i]):
        check[i] = False
if check.count(False) <= 2:
    print("YES")
else:
    print("NO")

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

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

print('YES' if cnt in (0, 2) else 'NO')

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

sorted_numbers = sorted(numbers)

diff = 0
for i in range(N):
    if numbers[i] != sorted_numbers[i]:
        diff += 1
        
if diff == 2 or diff == 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 i+1 != p[i]:
        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, num in enumerate(p):
    if i + 1 != num:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

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

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

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

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

Solution (Python):
input()
P=list(map(int,input().split()))
a=0
for p,q in zip(P,sorted(P)):
    if p!=q:
        a+=1
print("YES" if a<3 else "NO")

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

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

Solution (Python):
from operator import ne

N = int(input())
P = list(map(int, input().split()))
Q = sorted(P)
print("YES" if sum(map(ne, P, Q)) <= 2 else "NO")

Solution (Python):
n=int(input())
plist = list(map(int,input().split(" ")))
nlist = list()
count = 0
for i in range(n):
  nlist.append(i+1)
  
for j in range(len(nlist)):
  if plist[j]!=nlist[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()))

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

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

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

Solution (Python):
import copy

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

for i in range(n):
    for j in range(n):
        t[i], t[j] = t[j], t[i]
        if sorted(t) == t:
            print("YES")
            exit()
        t[j], t[i] = t[i], t[j]
        


print("NO")

Solution (Python):

N = int(input())
p = [int(i) for i in 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()))
cnt = 0
for num, val in enumerate(p):
    if val != num + 1:
        cnt += 1

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

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