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())
P = list(map(int,input().split()))
for i in range(N):
    if P[i] != i+1:
        P[P[i]-1] = P[i]
        P[i] = i+1
        break
if P == sorted(P):
    print('YES')
else:
    print('NO')

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

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

if 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 i + 1 == p[i]:
        res += 1

if res >= n - 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==0 or count==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)]

for i in range(n):
    for j in range(n):
        s = p.copy()
        tmp = 0
        tmp = s[i]
        s[i] = s[j]
        s[j] = tmp
        if s == t:
            print("YES")
            exit()
print("NO")

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

Solution (Python):
n, *P = map(int, open(0).read().split())
s = sum(i != p for i, p in enumerate(P, 1))
print('YES' if s <= 2 else 'NO')

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

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

p_list = [int(x) for x in input().split()]
q_list = sorted(p_list)

count = 0
ans = "YES"
for p, q in zip(p_list, q_list):
    if p != q:
        count += 1
        if count > 2:
            ans = "NO"
            break
print(ans)

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

UP = []
for i in range(N):
  UP.append(i+1)

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

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

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
sa = sorted(a)
if a==sa:
  print("YES")
else:
  for ai,si,i in zip(a,sa,range(n)):
    if ai!=si:
      a[ai-1],a[i]=a[i],a[ai-1]
      break
  if a==sa:
    print("YES")
  else:
    print("NO")

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

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

Solution (Python):

N = int(input())
p = [int(s) for s in 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 or diff == 0 else 'NO')

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

a=sorted(p)
b=0

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

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

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

n_l = [i for i in range(1, n+1)]
n_l.reverse()
n_l_a = list(reversed(n_l))

if (n_l_a == p):
    print('YES')
    sys.exit()
else:
    for i in range(n):
        for j in range(n):
            p[i], p[j] = p[j], p[i]
            if (n_l_a == p):
                print('YES')
                sys.exit()
            else:
                p[i], p[j] = p[j], p[i]

print('NO')

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

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

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 = [int(i) for i in input().split()]
c = 0
for i in range(n):
    if a[i] != i + 1:
        c += 1
    if c == 3:
        print("NO")
        break
else:
    print("YES")

Solution (Python):
n,*p=map(int,open(0).read().split())
q=sorted(p)
for i in range(n):
  for j in range(n):
    r=p[:]
    r[i],r[j]=r[j],r[i]
    if r==q:
      print('YES')
      exit()
print('NO')

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

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

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, groupby
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
p = LIST()

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

for i in range(N-1):
  for j in range(i+1, N):
    q = p.copy()
    q[i] = p[j]
    q[j] = p[i]
   
    if q == sorted(p):
      print("YES")
      exit()
      
print("NO")

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

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

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



count=0
for a,b in zip(p,q):
  if a!=b:
    count += 1


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

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

asc_list = sorted(p)
count = 0
for a, b in zip(p, asc_list):
    if a != b:
        count += 1

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

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

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

cnt = 0
for x, y in zip(p, p_sort):
    if x != y:
        cnt += 1

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1

""" Input
S = input().rstrip()
N, A, B = map(int, input().split())
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

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

p_sort = sorted(P)

counter = 0
for x, y in zip(P, p_sort):
    if x != y:
        counter += 1

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

Solution (Python):
def main():
    N = int(input())
    p = [int(a) for a in input().split()]
    p_ = sorted(p)
    cnt = 0
    for i in range(N):
        if p[i] != p_[i]:
            cnt += 1
    if cnt <= 2:
        print("YES")
    else:
        print("NO")
main()

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

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

Solution (Python):
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7


def main():
  N = int(readline())
  P = np.array(readline().split(), np.int64)
  A = np.array(range(1,N+1), np.int64)
  B = np.count_nonzero(P-A)
  if B==0 or B==2:
    ans = 'YES'
  else:
    ans = 'NO'
  print(ans)
  
  
  
  
      
if __name__ == '__main__':
  main()

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

cnt = sum([p1 != p2 for p1, p2 in zip(P, sorted((P)))])
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(0, N):
    if p[i] != i + 1:
        count += 1

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

Solution (Python):


import sys



















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    p = lmi()
    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 print('NO')

if __name__ == "__main__":
    main()

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

print("YES")

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("NO")
            break
else:
    print("YES")

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

for i in range(1, N + 1):
    if cnt == 3:
        print("NO")
        exit()
    elif i != p[i - 1]:
        cnt += 1

print("YES")

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

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
flag = 0
ans = True
for i in range(0,n):
    if p[i] != i+1 and flag<=2:
        flag+=1
        if p[p[i]-1] == i+1:
            continue
        else:
            ans = False
    elif i+1 != p[i]:
        ans = False
if ans:
    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:
    continue
  else:
    a += 1
    
if a > 2:
  print('NO')
else:
  print('YES')

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

wrong = 0
for i, p in enumerate(map(int, input().split()), start=1):
  wrong += int(i != p)

if wrong == 0 or wrong == 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:
    continue
  else:
    count += 1

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

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

Solution (Python):
n = int(input())
p = [int(e) for e in 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()))
B=sorted(p)
for i in range(N):
    if i+1==p[i]:
        pass
    else:
        index=p.index(i+1)
        p[i], p[index]=p[index], p[i]
        break
if p==B:
    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
        
if cnt <= 2:
    print('YES')
    
else:
    print('NO')

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

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

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

Solution (Python):
def is_sorted(t_list):
    for i in range(len(t_list)):
        if t_list[i] != i + 1:
            return False
    return True


n = int(input())
p_list = list(map(int, input().split()))
ret = False

if is_sorted(p_list):
    print('YES')
    exit()

for i in range(n):
    for j in range(i+1, n):
        p_list[i], p_list[j] = p_list[j], p_list[i]
        if is_sorted(p_list):
            print('YES')
            exit()
        else:
            p_list[i], p_list[j] = p_list[j], p_list[i]

print('NO')

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

sorted_p = sorted(p)

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

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

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

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

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

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

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

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

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

Solution (Python):
import numpy as np


n = int(input())
p = [i for i in list(map(int, input().split()))]
p = np.array(p)
p_org = p.copy()
p.sort()
if (p != p_org).sum() <= 2:
  print('YES')
else:
  print('NO')

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

for i in range(n):
    if x[i] != i+1:
        num_count += 1
        
if num_count == 0 or num_count == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys


def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")



n=II()
l=LI()

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

Solution (Python):
import sys
N=input()
P=map(int, sys.stdin.readline().split())
cnt=0
for x,y in zip(P,sorted(P)):
    if x!=y: cnt+=1

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

Solution (Python):
import io

nim = int(input())

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

kite = list(range(1,nim+1))

counter = 0

for i in range(nim):
    if kite[i]!=mike[i]:
        counter+=1
if counter<=2:
    print("YES")
else:
    print("NO")

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

if p == p_:
  print("YES")
  sys.exit()

for i in range(N):
  for j in range(i+1, N):
    a = p[i]
    p[i] = p[j]
    p[j] = a
    if p == p_:
      print("YES")
      break
    else:
      b = p[i]
      p[i] = p[j]
      p[j] = b
      continue
  else:
    continue
  break
else:
  print("NO")

Solution (Python):
N=int(input())
p=[int(i) for i in input().split()]
q=sorted(p)
count=0
for t in range(N):
    if p[t]!=q[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()))

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

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

Solution (Python):
N=int(input())
p_list=list(map(int, input().split()))
p_sort = []
for i in range(N):
  p_sort.append(p_list[i])
p_sort.sort()
ans = 0



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

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

Solution (Python):
import copy
N=int(input())
p=[int(i) for i in input().rstrip().split()]
p_c=copy.deepcopy(p)
p_c.sort()

cnt=0
for i in range(len(p)):
  if p[i]!=p_c[i]:
    cnt+=1
  else:
    continue


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

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


count=0
for i in range(N):
    if p[i]==i+1:
        continue
    else:
        dammy = p[i]
        num = p.index(i+1)
        p[i]=p[num]
        p[num] = dammy
        count+=1

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

Solution (Python):
N = int(input())
p = [int(x) for x in input().split()]
n = [int(y) for y in range(1, N+1)]
ans = 'YES'
count = 0
for i in range(N):
    if p[i] == n[i]:
        pass
    else:
        if count <= 1:
            count +=1
        else:
            ans = 'NO'
print(ans)

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

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

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

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

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

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

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
P=nl()
c=0
for i in range(1,N+1):
    if(P[i-1]!=i):
            c+=1

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

Solution (Python):
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
nlst = [x for x in range(1,n+1)]
lst = [0]*n

for i in range(n):
    if nlst[i] != p[i]:
        lst[i] = 1
    
print('YES' if sum(lst) in [0,2] else 'NO')

Solution (Python):

num = 0
tmp = [0,0,0,0]
N = int(input())
p = list(map(int,input().split()))
m = list(int(i) for i in range(1,N+1))
l = 0
if p == m:
  print("YES")
else:
  for k in range(N):
    if num >= 2:
      break
    if p[k] == m[k]:
      continue
    else:
      tmp[l] = p[k]
      tmp[l+1] = int(k+1)
      num+=1
      l+=2
  if tmp[1] == tmp[2]:
    print("YES")
  else:
    print("NO")

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

P = sorted(p)
cnt = 0

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

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

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

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

c = sorted(p)
count = 0

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

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

Solution (Python):
n, *p = map(int, open(0).read().split())

p_sort = sorted(p)

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

Solution (Python):
N = int(input())
pn = list(map(int, input().split()))
check = 0
for i in range(N):
    if pn[i] != i+1:
        check += 1
print('YES' if check <= 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())
P = list(map(int, input().split())) 

cnt = 0

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

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

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

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

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

if p == p_l:
    print("YES")

else:
    for i in range(n):
        if not p[i] == p_l[i]:
            count +=1

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

Solution (Python):
N,*P,=map(int,open(0).read().split())
A=[a!=b for a,b in zip(range(1,N+1),P)]
print('YNEOS'[sum(A)!=0 and sum(A)!=2::2])

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

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

Solution (Python):
def q2():
    N = int(input())
    P = [int(i) for i in input().split()]
    P_sorted = sorted(P)
    cnt = 0
    for i, p in enumerate(P):
        if P[i] - P_sorted[i] != 0:
            cnt += 1

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


if __name__ == '__main__':
    q2()

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

p_ = sorted(p)

number_of_different = 0
answer = 'YES'
for pi, pi_ in zip(p, p_):
    if pi is not pi_:
        number_of_different += 1

    if number_of_different >= 3:
        answer = 'NO'
        break

print(answer)

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

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

Solution (Python):
N = int(input())
pi = list(map(int, input().split()))
pi_sort = sorted(pi)
count = 0
for p, p_s in zip(pi, pi_sort):
    if p != p_s:
        count += 1

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
p = LIST()
if p == list(range(1, n + 1)):
    print('YES')
    exit()
for i in range(n):
    for j in range(i + 1, n):
        arr = deepcopy(p)
        arr[i], arr[j] = arr[j], arr[i]
        if arr == list(range(1, n + 1)):
            print('YES')
            exit()
print('NO')

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


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

Solution (Python):
a = int(input())
b = input().split()
count=0
for i in range(a):
    if int(b[i]) is not i + 1:
        count += 1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
p_sort = sorted(p)
count = 0
for a, b in zip(p, p_sort):
  if a == b:
    count += 1
if len(p) - count <= 2:
  print("YES")
else:
  print("NO")

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

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

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

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
  
def main():
  n = int(input())
  P = list(map(int,input().split()))
  
  ANS = sorted(P)
  
  for i in range(n-1):
    for j in range(i,n):
      DUMMY = P.copy()
      tmp = DUMMY[i]
      DUMMY[i] = DUMMY[j]
      DUMMY[j] = tmp
      
      if DUMMY == ANS:
        print('YES')
        exit()
  print('NO')
  
if __name__ == '__main__':
  main()

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N = int(input())
    P = input_nums()
    Q = sorted(P)
    diff = [1 for p, q in zip(P, Q) if p != q]
    if sum(diff) <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

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