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

Solution (Python):
import numpy as np

N =  int(input())
num_arr = np.array(list(map(int, input().split())))
right_num_arr = np.array(list(range(1,N+1,1)))

dif = [abs(i) for i in num_arr - right_num_arr]

if dif == [0]*N:
  print("YES")
elif (sorted(set(dif))[0] == 0)&(len(set(dif))==2):
  print("YES")
else:
  print("NO")

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

flag = 0
tmp = 0
for i in range(1, n + 1):
    if i != lst[i-1]:
        tmp = lst[i-1]
        flag += 1
        if flag > 2:
            print("NO")
            exit()
    

print("YES")

Solution (Python):
n = int(input())
*a, = 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('NO')
    exit()
print('YES')

Solution (Python):
import copy

if __name__ == '__main__':
    n = int(input())
    p = [int(i) for i in input().split()]
    p2=copy.copy(p)
    p2.sort()

    count=0
    p12=[]
    p22=[]
    for i in range(n):
        if p[i] != p2[i]:
            count = count+1
            p12.append(p[i])
            p22.append(p2[i])
    if (count == 2 and p12[0]==p22[1] and p12[1] == p22[0]) or count==0:
        print('YES')
    else:
        print('NO')

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

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

Solution (Python):
a=int(input())
l=list(map(int,input().split()))
ok=False
if sorted(l) == l:
    print("YES")
    exit(0)
for i in range(len(l)-1):

    for j in range(i+1,len(l)):
        aux=l[i]
        l[i]=l[j]
        l[j]=aux
        if sorted(l) == l:
            ok=True
        else:
            aux = l[j]
            l[j] = l[i]
            l[i] = aux
    if ok == True:
        print("YES")
        break
if ok ==False:print("NO")

Solution (Python):
INF = 100
n = int(input())
a = list(map(int, input().split()))
sorted_a = sorted(a)
count = 0
p1 = INF
p2 = INF
for i in range(n):
    if a[i] != sorted_a[i]:count+=1
    if p2==INF and p1!=INF:p2=i
    elif p1==INF:p1=i
if count < 2:
    print("YES")
    exit()
elif count == 2:
    temp = a[p1]
    temp2 = a[p2]
    rea = a
    rea[p1] = temp2
    rea[p2] = temp
    if a == rea:print("YES")
    exit()
print('NO')

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

s = sorted(p)
d = len([1 for i in range(n) if p[i] != s[i]])

if d in [0, 2] :
    print("YES")
else :
    print("NO")

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

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

p.insert(0, 0)

wrong_list = []
for i in range(1,N+1):
  if p[i] != i:
    wrong_list.append([i,p[i]])

if len(wrong_list) == 0:
  print("YES")
elif len(wrong_list) != 2:
  print("NO")
else:
  print("YES")

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

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

Solution (Python):
n = int(input())
import numpy as np
p = np.array(list(map(int, input().split())))
ps = np.array(sorted(p))
s = np.sum(p!=ps)
if s==0 or s==2:
  print("YES")
else:
  print("NO")

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

cnt = 0
for a, b in zip(p, p_sort):
  if a != b:
    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()))
p_copy=copy.copy(p)
p.sort()
for i in range(n):
  for j in range(i+1,n):
    p_copy[i],p_copy[j]=p_copy[j],p_copy[i]
    if p==p_copy:
      break
    else:
      p_copy[i],p_copy[j]=p_copy[j],p_copy[i]
  if p==p_copy:
    break
if p==p_copy:
  print('YES')
else :
  print('NO')

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

counter = 0
for pair in zip(p_list, range(1, len(p_list) + 1)):
    if pair[0] - pair[1] != 0:
        counter += 1

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

Solution (Python):
N = int(input())
list1 = list(map(int, input().split()))
list2 = list1.copy()
check_l = []
for i in range(len(list1)):
    if (i+1) != list1[i]:
        check_l.append(i)
if len(check_l) <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):

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

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

for i,j in zip(p, q):
    if i == j:
        continue
    else:
        count += 1
    if count > 2:
        print('NO')
        exit()
print('YES')

Solution (Python):
import sys
import collections
import copy
input = sys.stdin.readline
N = input()
P= list(int(i) for i in input().split())
chk = 0
col = []
for i in range (len(P)):
	if P[i] != i+1 :
		chk +=1
if chk ==2 or chk ==0:
	print("YES")
else:
	print("NO")

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

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

flg = False
if sp == p:
    print("YES")
else:
    for i in range(n):
        for j in range(i+1, n):
            cp = p.copy()
            cp[i], cp[j] = cp[j], cp[i]
            if cp == sp:
                print("YES")
                flg = True
                break
        if flg:
            break
    if not flg:
        print("NO")

Solution (Python):
import copy

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

flag = 0



if p_list == sorted(p_list):
  print("YES")
  flag = 1
  

if flag == 0:
  for i in range(N):
    for j in range(i, N):
      
      new_p_list = copy.deepcopy(p_list)
      tmp = new_p_list[j]
      new_p_list[j] = new_p_list[i]
      new_p_list[i] = tmp
      
      
      if new_p_list == sorted(p_list):
        print("YES")
        flag = 1
        break
      
if flag == 0:
  print("NO")

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

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

count = 0

for i in range(n):
  if ls[i] == i+1:
    continue
  else:
    count +=1
    if count >=2:
      break
    if ls[ls[i]-1]==i+1:
      ls[ls[i]-1] = ls[i]
      ls[i] = i+1

    else:
      count+=1

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

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

not_equal_count=0
for i in range(N):
  if P[i] != Q[i]:
    not_equal_count += 1
if not_equal_count in [0, 2]:
  print("YES")
else:
  print("NO")

Solution (Python):
import copy

n = int(input())

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

p_list = copy.deepcopy(p_list_org)

p_list.sort()

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

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

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

flag = True
swap_count = 0
for i in range(N):
    if p[i] != i+1:
        if swap_count == 0:
            j = p[i] - 1
            tmp = p[j]
            p[j] = p[i]
            p[i] = tmp
            swap_count += 1
        else:
            flag = False
            break

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

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

for i in range(N):
  for j in range(i,N):
    A[i],A[j]= A[j],A[i]
    for k in range(N):
      if A[k]!= B[k]:
        break
    else:
      print('YES')
      exit()
    A[i],A[j]= A[j],A[i]
        
print('NO')

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


sorted_list = sorted(num_list)
false_count = 0
for i, j in zip(num_list, sorted_list):
  if i != j:
    false_count += 1
    if false_count > 2:
      print('NO')
      break

if false_count <=2:
	print('YES')

Solution (Python):
sentence = input()
N = int(sentence)
sentence = input()
p_str = sentence.split()
p_list = []
previous = -1
need_move = []
for i, p in enumerate(p_str):
    present = int(p)
    p_list.append(present)
    if previous > present:
        need_move.append(i)
        need_move.append(i-1)
    previous = present
ans_ok = "YES"
ans_no = "NO"

if not need_move:
    print(ans_ok)
    exit()
if len(need_move) > 4:
    print(ans_no)
    exit()
while len(need_move) > 1:
    move_index = need_move.pop(0)
    for move_index_2 in need_move:
        
        before_number = p_list[move_index]
        after_number = p_list[move_index_2]
        p_list[move_index] = after_number
        p_list[move_index_2] = before_number
        ok = True
        pre = -1
        for p in p_list:
            if p < pre:
                ok = False
                break
            pre = p
        
        if ok:
            print(ans_ok)
            exit()
        p_list[move_index] = before_number
        p_list[move_index_2] = after_number
print(ans_no)

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

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

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

Solution (Python):


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

cnt = sum([1 if a!=b else 0 for a,b in zip(p, range(1, N+1))])


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

Solution (Python):
import numpy as np
N = int(input())
p = [int(i) for i in input().split()] 
a = np.zeros(N-1)
b = a
c = 0
d = 0
for j in range(N-1):
  a[j] = p[j+1] - p[j]
b = sorted(a)
if b[0] > 0:
  print('YES')
elif b[2] > 0 and b[0] == b[1]:
  print('YES')
else:
  print('NO')

Solution (Python):
import copy

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

sortedP = copy.copy(p)
sortedP.sort()

count =0
for i in range(N):
    if p[i] != sortedP[i]:
        count = 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_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):
n=int(input())  

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

d=[i for i in range(1,n+1)]
f=0
for i in range(n):
    if c[i]-d[i]:
        f+=1

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

Solution (Python):
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

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

sort = [i for i in range(1, N+1)]
t = 0
for i, n in enumerate(sort):
  if  P[i] != n and t < 2:
    t +=1
    continue
  elif P[i] != n and t > 1:
    print("NO")
    quit()
print("YES")

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

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

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

    if count > 2:
        flag = False
        print('NO')
        break


if flag:
    print('YES')

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

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

temp = 0

for i, j in enumerate(p):
    if nP[i] != j:
        temp += 1

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

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

Solution (Python):
import sys

stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)


def li(): return map(int, stdin.readline().split())


def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())


def lf(): return map(float, stdin.readline().split())


def ls(): return stdin.readline().split()


def ns(): return stdin.readline().rstrip()


def lc(): return list(ns())


def ni(): return int(stdin.readline())


def nf(): return float(stdin.readline())

N = int(input())
P_str = input().split()
P = [int(p) for p in P_str]

Q = sorted(P)
check = 0
for i in range(len(P)):
    if P[i] != Q[i]:
        check += 1

    if check > 2:
        print("NO")
        exit()

if check == 1:
    print("NO")
    exit()

print("YES")

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

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

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

Solution (Python):

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




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

count = 0
for i in range(N):
    if target_list[i] != 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()))

flag = True
t = []

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


if len(t) == 0 or len(t) == 2:
    ans = "YES"

else:
    ans = "NO"

print(ans)

Solution (Python):


N = int(input())
p = list(map(int, input().split()))
res = 0
for i, j in zip(range(1, N+1), p):
    if i != j:
        res = res + 1
    
print("YES" if res == 0 or res == 2 else "NO")

Solution (Python):

N = int(input())
p = list(map(int, input().split()))
count = 0
for i ,pi in enumerate(p):
    if i+1 != pi:
        count += 1

if count in [0,2]:
    print("YES")
else:
    print("NO")

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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




for i in range(len(p)):
    for j in range(i, len(p)):
        p2 = sorted(p)
        tmp = p2[i]
        p2[i] = p2[j]
        p2[j] = tmp
        if (p == p2):
            print("YES")
            exit()
print("NO")

Solution (Python):


n = int(input())
t = input().split()
p = []
for i in t:
    p.append(int(i))

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

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

C = [P[i - 1] == i for i in range(1, N + 1)].count(0)

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

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]
q = [0] * N
for i in range(N):
    q[i] = i + 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())
P = list(map(int, input().split()))

Ps = sorted(P)

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

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

cnt = 0
for i in range(N):
  if 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()))
a=sorted(p)

for i in range(n-1):
    for j in range(i,n):
        p[i],p[j]=p[j],p[i]
        if p==a:
            print("YES")
            exit()
        p[i],p[j]=p[j],p[i]
print("NO")

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

res = []
count = 0
for i in range(int(N)):
    res.append(i+1)
for i,j in zip(res,parents):
    if not i==j:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")

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

cnt = sum(1 for (x, y) in zip(xs, ys) if x != y)

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

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

diff_count = 0
diff_formar = 0
diff_latter = 0

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

if diff_count in [0,2]:
    print("YES")
else:
    print("NO")

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

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

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

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

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

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

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

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

Solution (Python):
import numpy as np
N=int(input())
L=list(map(int,input().split()))
R=[i+1 for i in range(N)]
C=[0 for i in range(N)]

for i in range(N):
  C[i]=L[i]-R[i]

D=np.array(C)
if np.count_nonzero(D)>2:
  print("NO")
else:
  print("YES")

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

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

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

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

A = 0
for i in range(N):
  if a[i] != i+1:
    A += 1
if A <= 2:
  print("YES")
else:
  print("NO")

Solution (Python):
n = input()

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

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


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

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

count = 0
for i, p in enumerate(P):

    
    
    if i+1 != P[i]:
        count += 1

    if count > 2:
        break

if count > 2:
    print('NO')

else:
    print('YES')

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


psort = sorted(p)

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

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

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

S = input()
S = S.split()

P = list(map(int, S))

P_sort = sorted(P)
count = 0


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

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

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

Solution (Python):
import copy
import sys
N=int(input())
P=list(map(int,input().split()))
for i in range(N):
    for j in range(N):
        Pt=copy.copy(P)
        Pt[j]=P[i]
        Pt[i]=P[j]

        tmp=Pt[0]
        for k,t in enumerate(Pt[1:]):
            if t>tmp:
                tmp=t
            else:
                break
            if k==N-2:
                print('YES')
                sys.exit()
            
print('NO')

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

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

Solution (Python):
from sys import stdin

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

a=0

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

if a==n or a==n-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] is not i+1:
        count += 1

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

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

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

Solution (Python):

import numpy as np

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

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

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

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

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

ans = 0
for i in range(n):
    if p[i] != i+1:
        ans += 1
        if ans > 2:
            break

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

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

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

Solution (Python):
n = int(input())
p = [int(_) for _ in input().split()]
flg = False
flg1 = True
for i in range(n):
    for j in range(n):
        flg1 = True
        q = p[:]
        q[i]=p[j]
        q[j]=p[i]
        for k in range(n):
            for l in range(n):
                if k < l and q[k]>q[l]:
                    flg1 = False
                    break
                if l==n-1 and k==n-1 and flg1:
                    flg = True
        
if flg:
    print('YES')
else:
    print('NO')

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

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

p_list_sorted = sorted(p_list)

diff_num = 0
for i in range(n):
    if (p_list[i] - p_list_sorted[i]) != 0:
        diff_num += 1

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

Solution (Python):
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  
  

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

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

q=sorted(p)

c=0
for i in range(len(p)):
    if p[i] != q[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(len(P)):
	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(map(int,input().split()))
_p=sorted(p)
c=0
for a,b in zip(p,_p):
    if a!=b:
        c+=1
print('YES' if c<=2 else 'NO')

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

ans = 'NO'
p_sorted = sorted(p)


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

if cnt < 3:
    ans = 'YES'
print(ans)

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

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

OK = False
for i in range(N):
    for j in range(N):
        P[i], P[j] = P[j], P[i]
        if P == Q:
            OK = True
        P[i], P[j] = P[j], P[i]
print("YES" if OK else "NO")

Solution (Python):

n = int(input())
l = list(map(int,input().split()))
count = 0

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

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

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

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

count = 0

for idx,v in enumerate(t):
  if count > 2:
    break
  if idx+1 != v:
    count += 1

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

Solution (Python):
N = int(input())
inputData = input().split()
p = [int(x) for x in inputData]
sorted_p = sorted(p)
diff_num = 0
for i in range(N):
    if p[i] != sorted_p[i]:
        diff_num += 1
if diff_num > 2:
    print("NO")
else:
    print("YES")

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

Solution (Python):
import numpy as np
n=input()
p=np.array([int(n) for n in input().split()])
s=sorted(p)
r=p-s
d=np.count_nonzero(r!=0)
if (d>2):
    print("NO")
else:
    print("YES")

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

Solution (Python):
N=int(input())
pn=list(map(int,input().split()[:N]))
an=list(range(1,N+1))

con=[(x-y)!=0 for (x,y) in zip(pn,an)].count(True)
if con==2 or con ==0:
    print('YES')
else:
    print('NO')

Solution (Python):
len_arr = int(input())
arr = [int(x) for x in input().split(' ')]
increasing = range(1, len_arr+1)

rep_inc = 0

for i, inc in zip(arr, increasing):
    if i != inc:
        rep_inc += 1

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

exit(0)

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

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

counts = []

for i in range(len(p)):
    if p[i] == i + 1:
        counts.append(i)

if len(counts) == N - 2:
    print('YES')
elif len(counts) == N:
    print('YES')
else:
    print('NO')

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

p = list(int(_) for _ in input().split()) 
q = p.copy()
p.sort(reverse=False)


cnt = 0

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

if cnt >= 3:
	print('NO')
else:
	print('YES')

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

l = sorted(p)
count = 0

for a, b in zip(p, l):
    if (a is not b): count += 1

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

Solution (Python):


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

diff_count = 0
for i, p_i in enumerate(p):
    if i + 1 != p_i:
        diff_count += 1

print('YES' if diff_count in [0, 2] else 'NO')

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

Solution (Python):


m=int(input())
n=input().split(" ")
for i in range(m):
    n[i]=int(n[i])
  
s=sorted(n,reverse=False)
d=0
for i in range(m):
    if not (n[i]==s[i]):
        d=d+1

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

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

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

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

count=0
for i in range(0,N):
  diff=S[i]-p[i]
  
  if diff!=0:
    count=count+1

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

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

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

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

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

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

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

sortP = sorted(P)
sumChk = sum([a != b for a, b in zip(P, sortP)])


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

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

origin_p = P[:]
sorted_p = sorted(P[:])




counter = 0
for a, b in zip(origin_p, sorted_p):
    if a == b:
        continue
    else:
        counter += 1
        
if counter <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
L = list(map(int, input().split(" ")))
X = sorted(L)
cnt = 0
for a,b in zip(L,X):
    if a != b:
        cnt += 1
        if cnt >= 3:
            print("NO")
            break
if cnt <= 2:
    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=k+1

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