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

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

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

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

Solution (Python):
"""
- https://atcoder.jp/contests/abc136/tasks/abc135_b
- reg:2≤N≤50
"""
n = int(input())
p = [int(e) for e in input().split()]

z = [i for i in range(n) if p[i] != (i+1)]
ans = "YES" if len(z) <= 2 else "NO"
print(ans)

Solution (Python):
def solve():
    n = int(input())
    p = list(map(int, input().split()))
    irregular = []
    for i in range(n):
        if p[i] != i + 1:
            irregular.append([i + 1, p[i]])
    if len(irregular) == 0 or (len(irregular) == 2 and irregular[0][1] == irregular[1][0] and irregular[0][1] == irregular[1][0]):
        return "YES"
    else:
        return "NO"


if __name__ == "__main__":
    print(solve())

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

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

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

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

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

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

cnt = 0
for i in range(len(p)):
    if p[i] != sorted_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()))
import copy
P=copy.deepcopy(p)
P.sort()
cnt=0
for i,j in zip(p,P):
        if i!=j:
                cnt+=1
print("YES") if cnt<=2 else print("NO")

Solution (Python):

n=int(input())
p=list(map(int,input().split()))
import copy
P=copy.deepcopy(p)
P.sort()
cnt=0
for i,j in zip(p,P):
        if i!=j:
                cnt+=1
if cnt<=2:
        print("YES")
else:
        print("NO")

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

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

Solution (Python):
N = int(input())
numbers = input().split()

counter = 0

for i in range(N):
    if int(numbers[i]) != i+1:
        counter+=1
        
if counter <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
n=input()
p=list(map(int,input().split()))
def func(i,j):
    if i == len(p) :
        return "YES"
    elif p[i]==i+1 :
        return func(i+1,j)
    elif p[p[i]-1]==i+1 and j<2 :
        j+=1
        return func(i+1,j)
    else :
        return "NO"
print(func(0,0))

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

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

diff = 0
for i in range(num):
    if (ls[i] != i+1):
        diff += 1

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

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


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

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

expect = sorted(ps)

count = 0
for i, num in enumerate(ps):
    if expect[i] != num:
        count += 1

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

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

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

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

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

cnt = 0
not_matches = []
for i, j in zip(data, line):
  if i != j:
    cnt += 1
  if cnt >= 3:
    print('NO')
    break
else:
  print('YES')

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

cnt = 0
not_matches = []
for i, j in zip(data, line):
  if i != j:
    cnt += 1
    not_matches.append(i)
    not_matches.append(j)
  if cnt >= 3:
    print('NO')
    break
else:
  if len(not_matches) == 0:
    print('YES')
  elif not_matches[0] == not_matches[3] and not_matches[1] == not_matches[2]:
    print('YES')
  else:
    print('NO')

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

Solution (Python):
_=int(input())
ps=list(map(int, input().split()))
cnt=0
for i, p in enumerate(ps, 1):
    if i != p:
        cnt += 1

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

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

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("NO" if c>2 else "YES")

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

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

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

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

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

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

rv = sorted(v)

f = []
for i in range(0, len(v)):
 if v[i] != rv[i]:
  f.append(1)
  if len(f) >2:
   print("NO"); exit()

print("YES")

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

count = 0
ans = "YES"
for i,p in enumerate(P,1):
	if count > 2:
		ans = "NO"
		break
	if i != p:
		count += 1
print(ans)

Solution (Python):
def main():
    NUM = input()
    
    numary = list(map(lambda x: int(x), input().split()))
    cnt = 0
    for i in range(len(numary)):
        if numary[i] != i + 1:
            cnt = cnt + 1

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

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
nums = list(map(int,input().strip().split()))
cnt = 0
for i, num in enumerate(nums):
  if (i+1) != num:
    cnt += 1

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

Solution (Python):


N = int(input())
Pi = list(map(int, input().split()))
Ps = sorted(Pi)

count = 0
for i in Pi:
    s = Ps.pop(0)
    if i != s:
        count += 1

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

Solution (Python):
import numpy as np
import copy

def main():
    n = int(input())
    ns = list(input().split(' '))
    ns = ns[0:n]
    ns = [int(i) for i in ns]

    ns2 = copy.deepcopy(ns)

    ns2.sort()

    count =0
    for i ,v in enumerate(ns):
        if v != ns2[i]:
            count =count + 1
    
    if count >= 3:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count < 3):
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count < 3):
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count == 2):
    print("YES")
elif(count == 0):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

def isInc(a, n):
    for i in range(n-1):
        if a[i] > a[i+1]:
            return False
    return True


n = int(input())

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

if isInc(a,n):
    print("YES")
    sys.exit(0)

for i in range(n):
    for j in range(i+1,n):
        a[i], a[j] = a[j], a[i]
        if isInc(a,n):
            print("YES")
            sys.exit(0)
        a[i], a[j] = a[j], a[i]

print("NO")

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


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

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

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

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

Solution (Python):
import sys
import itertools

def isAscending(p):
    temp = float('-inf')
    for e in p:
        if temp >= e:
            return False
        temp = e
    else:
        return True

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

if isAscending(p):
    print('YES')
    sys.exit(0)

for i, j in itertools.combinations(range(n), 2):
    p[i], p[j] = p[j], p[i]
    
    if isAscending(p):
        print('YES')
        break
    p[i], p[j] = p[j], p[i]
else:
    print('NO')

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

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

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

mao=sorted(li)
z=0
for i in range (0,len(li)):
  if (li[i]!=mao[i]):
    z=z+1

if (z==0 or z==2):
  print ("YES")

else:
  print("NO")

Solution (Python):
import itertools
import copy
N = int(input())
p = list(map(int,input().split()))
p_new = copy.copy(p)
t = 0
arg_combination = list(itertools.combinations(range(N),2))

for k in range(N-1):
	if p[k] > p[k+1]:
		t = 1
if t == 0:
	print("YES")
	exit()

for a in arg_combination:
	p_new = copy.copy(p)
	p_new[a[0]] = p[a[1]]
	p_new[a[1]] = p[a[0]]
	t = 0
	for k in range(N-1):
		if p_new[k] > p_new[k+1]:
			t = 1
	if t == 0:
		print("YES")
		exit()
print("NO")

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

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

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

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

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

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

Solution (Python):


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

count = 0
for i in range(N):
    if p[i] == pn[i]:
        pass
    else:
        count = count + 1

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

Solution (Python):
import numpy as np
n=int(input())
p=list(map(int,input().split()))
q=[i+1 for i in range(n)]
p=np.array(p)
q=np.array(q)
r=p-q
b=np.count_nonzero(r==0)
if b>=n-2:
  print('YES')
else:
  print('NO')

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

Solution (Python):
n =int(input())
p = tuple(map(int,input().split()))
q = sorted(p)
c = 0
for i in range(n):
  if p[i] != q[i]:
    c += 1
print('YNEOS'[c!=2 and c!=0::2])

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

result = li.copy()
result.sort()

cnt = 0

for x,y in zip(li,result):
    if x != y:
        cnt += 1

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

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

L = l.copy()

l.sort()

s = 0

for i, j in zip(L,l):
    if i != j : s+=1
def Y(a,t = 0):
    if a<t : print('YES')
    else : print('NO')
def y(a, t =0):
    if a==t : print('Yes')
    else : print('No')
Y(s,3)

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

a=[]

for i in range(len(li)-1):
  if li[i]>li[i+1]:
    if len(a)==0:
      a.append(i)
    else:
      a.append(i+1)
if len(a)>2:
  print('NO')
  exit()
else:
  a.append(len(li)-1)
  a.append(len(li)-1)
  temp=li[a[0]]
  li[a[0]]=li[a[1]]
  li[a[1]]=temp
for i in range(len(li)-1):
  if li[i]>li[i+1]:
    print('NO')
    exit()
print('YES')

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

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

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

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

import numpy as np

p_sort = sorted(p)

p = np.array(p)
p_sort = np.array(p_sort)

if np.count_nonzero(p != p_sort) == 2 or np.count_nonzero(p != p_sort) == 0:
    print("YES")
else:
    print("NO")

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

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

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

Solution (Python):



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


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


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

Solution (Python):
N = int(input())
P = [int(x) for x in input().split()]
R = sorted(P)
count = 0
for i in range(N):
    if P[i]!=R[i]:
        count +=1
if count >2:
    print('NO')
else:
    print('YES')

Solution (Python):
from copy import deepcopy

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

    count = 0

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

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys

a=int(input())
b=input().split() 
cnt=0

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

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

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

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

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

Solution (Python):
import sys

N, *P = map(int, sys.stdin.buffer.read().split())
Q = sorted(P)
count = 0
for x, y in zip(P, Q):
    if x != y:
        count += 1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

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

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

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

count=0
ans="YES"
for i in range(len(p)):
    if p[i]!=i+1:
        count+=1
    if count>=3:
        ans="NO"
        break

print(ans)

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

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

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):
n1 = int(input())

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

r1 = "YES"

n3 = sorted(n2)

c1 = 0

for i1 in range(n1):
    if n2[i1] != n3[i1]:
        c1 += 1
        if c1 == 3:
            r1 = "NO"

print(r1)

Solution (Python):
a  = int(input())
ls = [int(s) for s in input().split()]


total = 0

for i in range(a):
    if not ls[i] == i+1:
        total += 1

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

Solution (Python):

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

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

s_pnt = sorted(pnt)

cnt = 0
for p, s in zip(pnt, s_pnt):
    if p != s:
        cnt += 1


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

Solution (Python):



N = int( input() )
plist = list( map( int, input().split()))
sorted_list = sorted( plist )

diff = 0
for n in range( N ):
    if plist[ n ] != sorted_list[ n ]:
        diff += 1

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

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

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

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

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

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

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

k = 0
counter = 1
for e in p:
    if e == counter:
        counter += 1
    else:
        k += 1
        counter += 1

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

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

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

s = tuple(sorted(P))

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

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

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

for i in range(n):
    if numlist==sortedlist:
        print("YES")
        break
    minnum=i
    for j in range(i,n):
        if numlist[minnum]>numlist[j]:
            minnum=j
    if minnum!=i:
        a=numlist[minnum]
        numlist[minnum]=numlist[i]
        numlist[i]=a
        if numlist==sortedlist:
            print("YES")
        else:
            print("NO")
        break

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

cnt = 0
for i in range(N):
    if cnt > 2:
        break

    if Nlist[i] == (i + 1):
        continue
    else:
        cnt += 1

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

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

sorted_pp = pp[:]
sorted_pp.sort()

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

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

Solution (Python):
N = raw_input()
a = map(int, raw_input().split())
ind = 0
counter = 0

for num in a:
	ind = ind + 1
	if num != ind:
		counter = counter + 1
	
if counter < 3:
	print("YES")
else:
	print("NO")

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

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

if(cnt<3):
    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 - 1):
  if i+1 != p[i]:
    cnt = cnt + 1
if cnt / 2 > 1:
  print("NO")
else:
  print("YES")

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

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

r = sum([1 for i, j in zip(range(1, n+1), p) if i != j])
print("YES" if r == 0 or r == 2 else "NO")

Solution (Python):
N = input()
nList = input().split()

i = 1
checkSum = 0
for n in nList:
    if int(n) != i:
        checkSum += 1
    i += 1
if checkSum > 2 :
    print("NO")
else:
    print("YES")

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

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

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

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

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

sorted_list_ = sorted(list_)

count = sum([1 if l != r else 0 for l, r in zip(list_, sorted_list_)])

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 = count + 1
if count == 2 or count == 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):
N = int(input())
li = []
li = list(map(int, input().split()))

flag = True
for i in range(0, N):
  if i + 1 != li[i]:
    if flag == False:
      print("NO")
      break
    for j in range(i + 1, N):
      if li[j] == i + 1:
        li[i] = i + 1
        li[j] = j + 1
        flag = False
else:
  print("YES")

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

flag = True  
new = -1
m = -1
for i in range(num):
  if i+1 == a[i]:
    pass
  else:
    if new == -1:
      new = i
    else:
      a[i],a[new] = a[new], a[i]
      break

for i in range(num):
  if i+1 == a[i]:
    pass
  else:
    flag = False
    print('NO')
    break
if flag:
  print('YES')

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

for n, p in enumerate(P):
    if n+1 != p:
        count += 1
    if count > 2:
        print('NO')
        break
else:
    print('YES')

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

Solution (Python):

n = int(input())
a_list = list(map(int, input().split()))
b_list = range(1, n+1)
cnt = 0
for a, b in zip(a_list, b_list):
    if a != b:
        cnt+= 1
if cnt <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
array = list(map(int,input().split()))
ori = list(range(1,N+1))
check = [array[index] == ori[index] for index in range(N)]
flag = N - sum(check)
if flag <=2:
  print("YES")
else:
  print("NO")

Solution (Python):

N=int(input())
p=list(map(int,input().split()))
a=p[:]
p_u=[x for x in range(1,N+1)]

if p==p_u:
    print("YES")
else:
    for i in range(N):
        if p[i]-p_u[i]>0:
            a[p[i]-1]=p[i]
            a[i]=p[p[i]-1]
            if a==p_u:
                print("YES")
            else:
                print("NO")
                break

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

P = p.copy()
P.sort()
final = []

if P == p:
    final.append("YES")
else:
    for i in range(N):
        for o in range(i):
            pCopy = p.copy()
            I = pCopy[i]
            O = pCopy[o]
            pCopy[i] = O
            pCopy[o] = I
            if pCopy == P:
                final.append("YES")
            pCopy = p
if "YES" in final:
    print("YES")
else:
    print("NO")

Solution (Python):
"""
 // Author : snape_here - Susanta Mukherjee
 // Date : 27/07/2019

"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()

    


from math import ceil,floor,log,sqrt,factorial,pow,pi






  



def ii(): return int(input())
def fi(): return float(input())
def si(): return str(input())
def mi(): return map(int,input().split())
def li(): return list(mi())

def main():
    
    n=ii()
    p=li()
    l=sorted(p)
    c=0
    for i in range(n):
        if p[i]==l[i]:
            c+=1
    if c==n or c==n-2:
        print("YES")
    else:
        print("NO")
    
    


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")



if __name__ == "__main__":
    main()

Solution (Python):

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

diff_list = []
for i, num in enumerate(num_list):
    if not i+1 == num:
        diff_list.append([i+1, num])
    if len(diff_list) > 2:
        break
if len(diff_list) == 2 and diff_list[0][0] == diff_list[1][1] and diff_list[0][1] == diff_list[1][0]:
    print('YES')
elif len(diff_list) == 0:
    print('YES')
else:
    print('NO')

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

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

def is_asc(l, o):
    for o1, o2 in zip(o, o[1:]):
        if l[o1] > l[o2]:
            return False
    return True

if is_asc(pl, range(n)):
    print('YES')
else:
    can_asc = False
    for i in range(n):
        for j in range(i+1, n):
            o = list(range(n))
            o[i] = j
            o[j] = i
            if is_asc(pl, o):
                can_asc = True
                break
    if can_asc:
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
import copy
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
for i in range(n):
    for j in range(i, n):
        x = copy.copy(p)
        x[i], x[j] = x[j], x[i]
        y = sorted(x)
        if x == y:
            print("YES")
            exit()
print("NO")

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

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

Solution (Python):


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

flag = 0

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

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

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

count=0
for i in range(int(n)):
  if N[i] !=i+1:
    count+=1
if count > 2:
  print("NO")
else:
  print("YES")

Solution (Python):
n = input()
s = input().split()

s = [int(i) for i in s]

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

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