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()))
check = {}
cnt = 0
ans = [i+1 for i in range(n)]
for i in range(n):
    if(p[i]==ans[i]):
        continue
    elif(p[i]!=ans[i]):
        check[i] = p[i]
        if(cnt<3):
            cnt += 1
        else:
            break
if(cnt==0):
    print("YES")
elif(cnt==2):
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
check = {}
cnt = 0
ans = [i for i in range(n)]
for i in range(n):
    if(p[i]==ans[i]+1):
        continue
    elif(p[i]!=ans[i]):
        check[i] = p[i]
        if(cnt<3):
            cnt += 1
        else:
            break
if(cnt==0):
    print("YES")
elif(cnt==2):
    print("YES")
else:
    print("NO")

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

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

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

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

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

p2 = p.copy()
p2.sort()

p = np.asarray(p)
p2 = np.asarray(p2)
p3 = [p!=p2]
c = np.count_nonzero(p3)
if c == 2 or c == 0:
    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 i != p[i - 1]:
        cnt += 1
if cnt <=2:
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
p = list(map(int, input().split(" ")))
     
ps = sorted(p)
     
c = len([1 for x,y in zip(p, ps) if x != y])
if c == 2 or c == 0:
  print("YES")
else:
  print("NO")

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

Solution (Python):
n = input()
list = [int(x) for x in input().split()]
end = 0
for i, num in enumerate(list):
    if num != i + 1:
        end += 1
    if end > 2:
        break

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

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

diff = 0
for p, sp in zip(P, sort_P):
    if p != sp:
        diff += 1
        
if diff == 2 or diff == 0:
    print('YES')
else:
    print('NO')

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

swapped = False
result = 'YES'
for i in range(N):
  if p[i] == i + 1:
    continue
  
  temp = p[i]
  if swapped or p[temp - 1] != i + 1:
    result = 'NO'
    break
  
  p[i] = p[temp - 1]
  p[temp - 1] = temp
  swapped = True
      
print(result)

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

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

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

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

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

for j in range(0, n):
    if p[j] != asce[j]:
        dif.append(j)

if len(dif) == 2:
    if p[dif[1]] == asce[dif[0]] and p[dif[0]] == asce[dif[1]]:
        print("YES")
    else:
        print("NO")
else:
    if len(dif) == 0:
        print("YES")
    else:
        print("NO")

Solution (Python):
n = int(input())
p = [int(i) for i in 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):
n=int(input())
p=list(map(int,input().split()))
s=sorted(p)
flag=0
from copy import deepcopy as dc
if p==s:
    flag=1
for i in range(n):
    for j in range(i+1,n):
        d=dc(p)
        d[i],d[j]=d[j],d[i]
        if d==s:
            flag=1
if flag==1:
    print("YES")
else:
    print("NO")

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

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

P = list(map(int, input().split()))
P_sort = sorted(P)
cnt = 0

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

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

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

Solution (Python):


import sys
import copy

sys.setrecursionlimit(1000000)


from collections import defaultdict



N = int(input())

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

p.sort()

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

print(("NO", "YES")[diff<=2])

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

f = False
for i in range(n-1):
    for j in range(i+1, n):
        q = p[:]
        b = q.pop(j)
        a = q.pop(i)
        q.insert(i, b)
        q.insert(j, a)
        r = sorted(q[:])
        if q == r:
            f = True
            break
    if f:
        break

if p == sorted(p):
    f =True

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

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

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

Solution (Python):
import sys
N = int(input())
As = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(As):
    if i+1 != a:
        if As[a-1] == i+1:
            cnt +=1
        else:
            print("NO")
            sys.exit()

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

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

count = 0

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

Solution (Python):




import math
import numpy as np
import statistics


n = int(input())

p = list(int(i) for i in input().split())

cnt = 0
l = np.arange(n) + 1
for i in range (n):
  if not(p[i] == l[i]):
    cnt += 1
if(cnt in [0,2]):
  print('YES')
else:
  print('NO')

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

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

cnt = 0
idx = []
for i in range(N):
    if p[i] != ps[i]:
        cnt += 1
        idx.append(i)

if cnt == 2:
    i,j = idx[0],idx[1]
    p[i],p[j] = p[j],p[i]
    if p == ps:
        print('YES')
    else:
        print('NO')
elif cnt == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ansa=sorted(A)
ans=0
for i in range(N-1):
  if ans==0:
    if A[i]>A[i+1]:
      ch=A[i]
      m=i
      ans=ans+1
  elif ans==1:
    if A[i]>A[i+1]:
      n=A[i+1]
      A[i+1]=ch
      A[m]=n
if A==ansa:
  print("YES")
else:
  print("NO")

Solution (Python):

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

c = 0

for i in range(1, N + 1):
    if pn[i - 1] != i:
        c += 1

ans = 'YES' if c <= 2 else 'NO'

print(ans)

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

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math






def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
	N = int(input())
	
	
	PP = list(map(int, input().split(" ")))
 
	flg = True
	for k,val in enumerate(PP):
		if k == 0:
			continue
		if PP[k-1] <= val:
			pass
		else:
			flg = False
 
	if flg == True:
		print("YES")
		sys.exit()
 
	tmp = []
	for i in range(N):
		for j in range(i+1, N):
			P = copy.deepcopy(PP)
			tmp = copy.deepcopy(P[i])
			P[i] = copy.deepcopy(P[j])
			P[j] = copy.deepcopy(tmp)
			
			flg = True
			for k,val in enumerate(P):
				if k == 0:
					continue
				if P[k-1] <= val:
					pass
				else:
					flg = False
			
			if flg == True:
				print("YES")
				sys.exit()
 
	print("NO")
            	
if __name__ == "__main__":
	main()

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

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

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

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

p.sort()

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

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

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

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

counter =0

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
temp = [number for number in p]
for i in range(N):
    if temp[i] != i + 1:
        temp[temp[i]-1], temp[i] = temp[i], temp[temp[i]-1]
        break
p.sort()
if temp == p:
    print("YES")
else:
    print("NO")

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

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

for count, i in enumerate(p):
    if(i != sort_p[count]):
        count1 = count1 + 1


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

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

base = [x for x in range(1, N + 1)]

count = 0
for n in range(N):
	count += (A[n] != base[n])

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

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

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

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

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

Solution (Python):
def test(x, j):
    
    A = j.split()
    numbers = []
    result = True
    for i in A:
        numbers.append(int(i))
    result = sorted(numbers)
    if (result == True):
        print("YES")
        return "YES"
    if (result == False):
        
        for i in range(len(numbers)):
            for x in range(i+1, len(numbers)):
                numbers2 = numbers[:]
                if swap(numbers2, i, x) == True:
                    print("YES")
                    return "TRUE"
    print("NO")
    return "FALSE"


def sorted(list):
    result = True
    for i in range(len(list)):
        if i < len(list) - 1:
            if (list[i] > list[i+1]):
                result = False
    return result


def swap(list, pos1, pos2):
    list[pos1], list[pos2] = list[pos2], list[pos1]
    return sorted(list)


N = int(input())
p = input()
test(N, p)

Solution (Python):

INPUT_SENTENCES = 2
ss = [input() for i in range(INPUT_SENTENCES)]
N = int(ss[0])
p = list(map(lambda x: int(x), ss[1].split()))
rev_index = []
for i in range(N-1):
    if p[i] > p[i+1]:
        if len(rev_index) > 0:
            rev_index.append(i+1)
        else:
            rev_index.append(i)
if len(rev_index) == 2:
    p[rev_index[0]], p[rev_index[1]] = p[rev_index[1]], p[rev_index[0]]
if p == list(range(1,N+1)):
    print("YES")
else:
    print("NO")

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

Solution (Python):

"""
Created on Mon Jul 29 02:06:58 2019

@author: matsui
"""


N=int(input())


P=input().split()

count=0
for i in range(N):
    if i+1 != int(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()))

ps = sorted(p)

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

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

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

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

ps = sorted(p)

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

if p == ps:
    print('YES')
else:
    count = 0
    for i in range(len(ps)):
        if ps[i] != p[i]:
            count += 1

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

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

sp = sorted(p)
same_count = len([1 for x, y in zip(p, sp) if x == y])
print('YES' if N - 2 <= same_count else 'NO')

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]

count = sum([x != i for i, x in enumerate(p, 1)])

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

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

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

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

Solution (Python):
def solve():
  n = int(input())
  p = list(int(i) for i in input().split())
  checker = 0
  query = [int(i+1) for i in range(n)]
  for i in range(n):
    if p[i] != query[i]:
      checker += 1
  if checker >2 :
    print("NO")
  elif checker ==1:
    print("NO")
  else:
    print("YES")
solve()

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


c = 0
is_ok = True
for i in range(N):
    if (i+1 != p[i]) :
        c += 1
    
    if (c > 2) :
        is_ok = False
        break

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

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

c = 0
for i, v in enumerate(arr):
  if i+1!=v:
    c += 1
print("YES" if c==2 or c==0 else "NO")

Solution (Python):
def isOne_sort(array):
    n = len(array)
    sort_count = 0
    for i in range(0, n-1):
        if(sort_count>1):
            return 'NO'
        
        min = i
        for j in range(i+1, n):
            if array[min] > array[j]:
                min = j
        
        if(i!=min):
            tmp = array[min]
            array[min] = array[i]
            array[i] = tmp
            sort_count+=1
    return 'YES'

N = int(input())
P = [int(i) for i in input().split()]
print(isOne_sort(P))

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

Solution (Python):
from sys import stdin


def main():
    _ = int(input())
    p_lists = [int(x) for x in stdin.readline().rstrip().split()]

    new_p_lists = sorted(p_lists)
    count = 0

    for p, new_p in zip(p_lists, new_p_lists):
        if p == new_p:
            pass
        else:
            count += 1

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


if __name__ == "__main__":
    main()

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

judge = [(v != k+1) for k, v in enumerate(p)]
if sum(judge) <= 2:
    print('YES')
else:
    print('NO')

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline
def intinput():return map(int,input().split())


n=int(input())
a=list(intinput())
b=a[:]
b.sort()
if a == b:
    print("YES")
    sys.exit()
for i in range(n):
    for j in range(i+1,n):
        a[i],a[j]=a[j],a[i]
        if a==b:
            print("YES")
            sys.exit()
        a[i],a[j]=a[j],a[i]
print("NO")

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

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

Solution (Python):
def main37():
    num = int(input())
    data = input().split()
    retu = []
    for i in range(num):
        retu.append(int(data[i]))
    kari = list(retu)
    kari.sort()
    kosu = 0
    for i in range(num):
        if retu[i] != kari[i]:
            kosu = kosu + 1
    if kosu == 2 or kosu == 0:
        print("YES")
    else:
        print("NO")

if __name__ == "__main__":
    main37()

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

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

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

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

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

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

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

Solution (Python):
N=int(input())
p_list=[int(i) for i in input().split()]
p_sort=sorted(p_list)
swap_index=[]
for i,x,y in zip(range(len(p_list)),p_list,p_sort):
    if x!=y:
        swap_index.append(i)
if len(swap_index)==0:
    print("YES")
elif len(swap_index)!=2:
    print("NO")
else:
    i,j=swap_index
    tmp=p_list[i]
    p_list[i]=p_list[j]
    p_list[j]=tmp
    if p_list==p_sort:
        print("YES")
    else:
        print("NO")

Solution (Python):
N=int(input())
p = [int(x) for x in 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()))

q = p.copy()
q.sort()


notequal = 0

for i,v in enumerate(p):
    if v != q[i]:
        notequal += 1

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

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

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

p_sort = sorted(p)

cnt = 0

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

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

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

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

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

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

sp = list(sorted(P))

cnt = 0
for a, b in zip(P, sp):
    if a != b:
        cnt += 1

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

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

ss=list(range(1,n+1))
cnt=0
for p,s in zip(ps,ss):
	if p!=s:
		cnt+=1
		
if cnt<=2:
	print('YES')
else:
	print('NO')

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

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

    NumList = [int(i) for i in input().split(' ')]
    diffcount = 0

    for i in range(N-1):
        
        if(i + 1 != NumList[i]):
            diffcount += 1

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

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
Y = [int(x) for x in input().split()]
Y_ = Y.copy()
Y_.sort()
test = [Y[i] == Y_[i] for i in range(len(Y))]
if test.count(False) <= 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
if cnt<=2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
plis = list(map(int, input().split()))
plis_sorted = plis.copy()
plis_sorted.sort()

if plis == plis_sorted:
	print("YES")
	exit()

for i in range(N):
	for j in range(i+1,N):
		ptmp = plis.copy()
		tmp = ptmp[i]
		ptmp[i] = ptmp[j]
		ptmp[j] = tmp
		if ptmp == plis_sorted:
			print("YES")
			exit()

print("NO")

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

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

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

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

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

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

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


if __name__ == "__main__":
    main()

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

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

if j == 0:
	print("YES")
	exit()
loc = p[swap1[0] - 1]
p[swap1[0] - 1] = swap1[0]
p[swap1[1]] = loc

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

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

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

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

for i in range(n):
    for j in range(i,n):
        p[i],p[j] = p[j],p[i]
        if q == p:
            print('YES')
            exit()
        p[i],p[j] = p[j],p[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:
    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+=0
    else:
        count+=1
if count<=2:
    print("YES")
else:
    print("NO")

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

Q = sorted(P)
n = sum(1 for p, q in zip(P, Q) if p != q)
yes = n == 0 or n == 2
print('YES' if yes else 'NO')

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

count = 0

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

Solution (Python):
import copy

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

if all([li[i] < li[i+1] for i in range(n-1)]):
    print('YES')
    exit()
for i in range(n):
    for j in range(i+1,n):
        a = copy.copy(li)
        a[i],a[j] = a[j],a[i]
        if all([a[i] < a[i+1] for i in range(n-1)]):
            print('YES')
            exit()

print('NO')