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 = input()
l = input().split(" ")
ns = []
k = 0
for i in range(int(n)):
  ns.append(i+1)
  if int(l[i]) != ns[i]:
    k += 1
if k <= 2:
  print("YES")
else:
  print("NO")

Solution (Python):
import numpy as np

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

N_array = np.arange(1, N+1, dtype=np.int)

count = 0

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

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

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

count=0
for i in range(n):
    if p[i]!=sort_p[i]:
        count+=1
    if count>2:
        print('NO')
        exit()
print('YES')

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

diff = 0
for i,j in zip(range(1, n+1), p):
    if i != j:
        diff += 1
if diff <= 2:
    print("YES")
else:
    print("NO")

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

args = [int(e) for e in input().split()]

count = 0

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

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

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

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

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

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

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

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

Solution (Python):
import math

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

p_dash = p.copy()
p_dash.sort()

count = 0

for i in range(n):
	if p[i] - p_dash[i] != 0:
		count += 1
	if count == 3:
		flag = False
		break

if count <= 2:
	flag = True

if flag:
	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 == 0:
    print('YES')
elif ans == 2:
      print('YES')
else :
    print('NO')

Solution (Python):
import copy

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

has_changed = False
for i in range(N):
    expect = i + 1
    
    
    if p[i] != expect:
        if has_changed:
            print('NO')
            exit()
        index = p[i] - 1
        if p[index] == expect:
            has_changed = True
            tmp = copy.deepcopy(p[i])
            p[i] = expect
            p[index] = tmp
            
        else:
            print('NO')
            exit()

print('YES')

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  


def solve(N: int, p: "List[int]"):
    s = 0
    for i, j in zip(p, sorted(p)):
        if i != j:
            s += 1
    if s == 0 or s == 2:
        print('YES')
    else:
        print('NO')
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(N)]  
    solve(N, p)

if __name__ == '__main__':
    main()

Solution (Python):
main_num = raw_input()
num_list = map(int, raw_input().split())

fake_counter = 0
counter = 0

while True:
  if num_list[counter] != counter + 1:
    fake_counter += 1
  counter += 1
  if counter >= int(main_num):
    break

if fake_counter > 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)]
cnt = 0
for i, j in zip(p, q):
    if i != j:
        cnt += 1
print('YES' if cnt == 0 or cnt == 2 else 'NO')

Solution (Python):

    
import sys

if sys.platform =='ios':
	sys.stdin=open('input_file.txt')
	
n=int(input())
l=list(map(int,input().split()))
lsort=sorted(l)
cnt=0

for i in range(n):
	if l[i]==lsort[i]:
		cnt+=1
		
if cnt==n or cnt==n-2:
	print("YES")
	
else:
	print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
sort_p = sorted(p)
count = 0
for origin, sort in zip(p, sort_p):
    if origin != sort:
        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 > 2:
    print("NO")
else:
    print("YES")

Solution (Python):
import copy

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

for i in range(N):
    if count > 2:
        print("NO")
        break
    if P[i] != i+1:
        count += 1
if count <=2:
    print("YES")

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

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

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

Solution (Python):
while True:
    try:
        n = int(input())
        N = list(map(int, input().split()))
        swap = 0
        for i in range(len(N)):
            if N[i] != i+1:
                N[N.index(i+1)] = N[i]
                N[i] = i+1
                swap += 1
                if swap > 1:
                    print("NO")
                    break
        else:
            print("YES")
            
    except:
        break

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

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

seq_sort = sorted(seq)
count = 0
for i in range(num):
  if seq[i]!=seq_sort[i]:
    count += 1

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

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

Solution (Python):

N = int(input())

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

collect = sorted(p)

if p == collect:
    print('YES')
    exit()

for i in range(N):
    for j in range(N):
        p[i],p[j] = p[j],p[i]
        if p == collect:
            print('YES')
            exit()
        p[i],p[j] = p[j],p[i]

print('NO')

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

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

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

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

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

Solution (Python):
N = int(input())
*P, = map(int, input().split())
n_diff = 0
for i, p in enumerate(P, 1):
    if i != p: n_diff += 1
print('YES' if n_diff <= 2 else 'NO')

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

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

Solution (Python):
import copy
import numpy as np
N=input()
N=int(N)
a=list(map(int,input().split()))
b=copy.copy(a)
a.sort(reverse=False)
c=np.array(a)-np.array(b)
NN=np.count_nonzero(c==0)
if N-NN<=2:
    print("YES")
else:
    print("NO")

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

Solution (Python):

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

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

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

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

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

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

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

Solution (Python):


N=int(input())
p=list(map(int,input().split()))
Asc=sorted(p)
check=0
for i in range(N):
    if Asc[i]==p[i]:
        pass
    else:
        check+=1
    if check>2:
        print('NO')
        exit()
print('YES')

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp = num[:]
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp = num.copy()
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
flag = False
for i in range(n):
    for j in range(n):
        q = p.copy()
        q[i], q[j] = q[j], q[i]
        if q == sorted(q):
            flag = True
            break
if flag:
    print("YES")
else:
    print("NO")

Solution (Python):


n = int(input())
l = list(map(int, input().split()))
c = 0
for i,n in enumerate(l,1):
    if n != i:
        c += 1
ans = 'YES' if c == 0 or c == 2 else 'NO'
print(ans)

Solution (Python):
if __name__ == '__main__':
    N =  int(input())
    p = list(map(int, input().split()))
    
    k_num = [x for i, x in enumerate(p, 1) if x != i]
    if len(k_num) <= 2:
        print('YES')
    else:
        print('NO')

Solution (Python):
from sys import exit, stderr, stdin
input = stdin.readline


def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    p = [int(n) for n in input().split()]
    sp = sorted(p)
    cnt = 0
    for i in range(N):
        if p[i] != sp[i]:
            cnt += 1

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





if __name__ == "__main__":
    main()

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

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

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

for i in range(n):
  moto.append(i+1)

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

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

Solution (Python):
import numpy as np

N = int(input())
p = list(map(int, input().split()))
sort_p = sorted(p)
if p == sort_p:
    print('YES')
else:
    p = np.array(p)
    sort_p = np.array(sort_p)
    index = np.where(p != sort_p)
    if len(p[index]) == 2:
        p[index[0][0]],p[index[0][1]] = p[index[0][1]],p[index[0][0]]
        p = list(p)
        sort_p = list(sort_p)
        if p == sort_p:
            print('YES')
        else:
            print('NO')
    else:
        print('NO')

Solution (Python):
import copy
N = int(input())
p = list(map(int,input().split()))
a = sorted(p)
if a==p:
  print("YES")
  exit()
for i in range(N):
  for j in range(i,N):
    b = copy.deepcopy(p)
    l = b[i]
    b[i] = b[j]
    b[j] = l
    if a==b:
      print("YES")
      exit()
print("NO")

Solution (Python):
N = int(input())
P = [int(_) for _ in input().split()]
P2 = sorted(P)
cnt = sum([1 for p in zip(P, P2) if p[0] != p[1]])
print('YES' if cnt <= 2 else 'NO')

Solution (Python):
import numpy as np

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

t = np.array([i for i in range(1, N+1)])

c = np.count_nonzero(p != t)

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

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

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

if len([x for x, y in zip(li_p, sorted(li_p)) if x != y]) <= 2:
    print("YES")
else:
    print("NO")

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

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

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

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

Solution (Python):
n = int(input())
p = list(int(i) for i in input().split())
q = list(range(1, n+1))
diff = [1 if i==j else 0 for i, j in zip(p,q)]
if diff.count(0) in [0, 2]:
    print('YES')
else:
    print('NO')

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

res = 0
a_sort = sorted(a)
 
for i in range(n):
    if a_sort[i] == a[i]:
        res += 1
 
if res >= n - 2:
    print("YES")
else:
    print("NO")

Solution (Python):
a = int(input())
b = input().split()
for i in range(a):
  b[i] = int(b[i])
  
for i in range(a-1):
  if (b[i] > b[i+1]):
    break
if i == a-2:
  print('YES')
else:
  for j in range(a-1,i,-1):
    if (b[j] < b[j-1]):
      break
  temp = b[i]
  b[i] = b[j]
  b[j] = temp
  for k in range(a-1):
    if (b[k] > b[k+1]):
      break
  if k == a-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 += 1

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

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

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

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

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

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

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

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

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

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

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

def judge(p, n):
	count = sum([1 for i in range(n) if p[i] != i + 1])
	return count <= 2

print('YES' if judge(p, n) else 'NO')

Solution (Python):
n = int(input())
p = [int(_) for _ in input().split()]
diff = sum(1 for i, j in zip(range(1, n + 1), p) if i != j)

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

Solution (Python):


import numpy as	np

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

n_sorted = np.arange(1, n+1)
correct = n_sorted == p
if correct.sum() >= n-2:
    print("YES")
else:
    print("NO")

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    p = list(map(int, input().rstrip('\n').split()))
    q = sorted(p)
    cnt = 0
    for i in range(n):
        if q[i] != p[i]:
            cnt += 1
    print("YES" if cnt <= 2 else "NO")


if __name__ == '__main__':
    slove()

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


for i in range(N):
    if ans[i] != p[i]:
        cot += 1
print("YES" if cot <= 2 else "NO")

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

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

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

Solution (Python):

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

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
cnt = 0
for x,y in zip(a, b):
    if x != y:
        cnt += 1
if cnt == 2 or a == b:
    print('YES')
else:
    print('NO')

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

psort = sorted(p)
a = 0

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

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

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

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

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

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

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

if check <= 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[i] != p_sort[i]:
        cnt += 1

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

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

L=[]
for i in range(n):
    if i+1 !=p[i]:
        L.append([i, p[i]])
    if len(L)==2:
        break


if len(L)<=1:
    print('YES')
else:
    p[L[0][0]] = L[1][1]
    p[L[1][0]] = L[0][1]
    if p == sorted(p):
        print('YES')
    else:
        print('NO')

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

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

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


pp = sorted(p)
c=0
for i in range(n):
    if str(pp[i]) == str(p[i]):
        c=c
    else:
        c=c+1

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

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

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

Solution (Python):
n = list(range(1,int(input())+1))
p = list(map(int,input().split()))
count = 0
ans = 'NO'

for i in range(len(n)):
  if n[i] == p[i]:
    continue
  else:
    count+=1
if count == 0 or count == 2:
  ans = 'YES'
print(ans)

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

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
res = 'YES'
count = 0
for i in range( n):
	if l[i] >  i + 1:
		p = l[i] - 1
		if l[p] == i + 1:
			l[i] = i
			l[p] = p + 1
			count += 1
			if count > 1:
				res = 'NO'
		else:
			res = 'NO'

print(res)

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

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


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

cnt = 0

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

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

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

lst = list(map(int,input().split()))
cnt = 0

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

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

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

lst = list(map(int,input().split()))
cnt = 0

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

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

Solution (Python):
import copy
import sys
n = int(input())
lst1 = list(map(int,input().split()))
lst2 = copy.copy(sorted(lst1))

c=0
for i in range(n):
    if lst1[i]==lst2[i]:
        pass
    else:
        c+=1
        if c > 2:
            print("NO")
            sys.exit()
            
print("YES")

Solution (Python):
import sys
N = int(input())
p = list(map(int,input().split()))
l = [0]*3
a = 0
for i in range(1,N+1):
    if p[i-1] != i:
        l[a] = i-1
        a += 1
        if a==3:
            print("NO")
            sys.exit()
temp = p[l[0]]
p[l[0]] = p[l[1]]
p[l[1]] = temp
for i in range(1,N+1):
    if p[i-1] != i:
        print("NO")
        sys.exit()
print("YES")

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

Solution (Python):
import sys

N = int(input())
p = list(map(int,input().split()))
p_sorted = sorted(p)
cnt = 0
for i in range(N):
    if p[i] != p_sorted[i]:
        cnt += 1
    if cnt==3:
        print("NO")
        sys.exit()

print("YES")

Solution (Python):
n=int(input())
model=[]
for i in range(n):
  model.append(i+1)

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

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

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

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

c=0
m = 0

for i in d:
  m = m+1
  if i != m:
    c = c+1
    if c>2:
      print("NO")
      exit()
print("YES")

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

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

Solution (Python):
N = int(input())
p = input().split()
P = sorted(p, key = float)
w = 0
i = 0
while i <= N-1:
  if p[i] == P[i]:
    i += 1
  
  else:
    w += 1
    i += 1
    
if w == 0 or w == 2:
  print('YES')
  
else:
  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 sorted_p[i] != p[i]:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
i = 0
k = 0
for x in A:
  i += 1
  if i == x:
    k += 0
  else:
    k += 1
if k <= 2:
  print('YES')
else:
  print('NO')

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

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

Solution (Python):
from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))


graph = defaultdict(int)
for i in range(n):
	graph[i+1] = arr[i]

root_arr = [i for i in range(n+1)]

def dfs(src):
	global visited
	stack = [src]
	visited.add(src)
	cycle_size = 0
	while stack:
		v = stack.pop()
		if not graph[v] in visited:
			visited.add(graph[v])
			stack.append(graph[v])
			cycle_size += 1
	return cycle_size

visited = set()
count = 0
for ele in arr:
	if not ele in visited:
		cs = dfs(ele)
		count += cs
print("YES" if count <= 1 else "NO")

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

sorted_arr = sorted(arr)

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

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

num_sort = sorted(num)
ans = 0

for i in range(N):
  if num[i] == num_sort[i]:
    continue
  else:
    ans += 1
    
if ans <= 2:
  print("YES")
else:
  print("NO")

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

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

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

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

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

if sum( x != 0 for x in delta) == 2 or sum( x != 0 for x in delta) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
cnt = 0
for a, b in zip(p, sorted_p):
    if a != b:
        cnt += 1
if cnt > 2:
    print("NO")
else:
    print("YES")

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

Solution (Python):
import sys
from copy import deepcopy
from collections import deque
sys.setrecursionlimit(20000000)


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

    count = 0

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

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


if __name__ == '__main__':
    main()

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

count = 0
for i, j in enumerate(p):
    if i != j-1:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

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

p_sort=sorted(p_list)
cnt=0
for a,b in zip(p_list,p_sort):
    if a!=b:
        cnt+=1

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

Solution (Python):

import numpy as np

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

p_ori = p[:]

p.sort()
p_sorted = p[:]

if p_ori == p_sorted:
    print("YES")
else:
    p_ori = np.array(p_ori)
    p_sorted = np.array(p_sorted)
    p_diff = p_ori - p_sorted

    if(sum(p_diff != 0) == 2):
        if p_diff.sum() == 0:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Solution (Python):
input();p=map(int,input().split())
diff=0
for idx,i in enumerate(p,1):
    if idx!=i:
        diff+=1
print("YES"if diff==0 or diff==2 else"NO")

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

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

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

q = []
for x in range(N):
    q.append(x+1)

i = 0
c = 0
ans = "NO"
while(i < N):
    if p[i] != q[i]:
        c += 1
    if c > 2:
        break
    i += 1

if (c == 2) or (c == 0):
    ans = "YES"

print(ans)

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

i=1
count=0
for item in r:
  if item!=i:count+=1
  i+=1

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

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

cnt = 0
for i in range(n) : 
    if P[i] != C[i] : 
        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()]
sor=[i for i in range(1,n+1)]
miss=0
for i in range(n):
    if p[i]!=sor[i]:
        miss+=1
print("YES" if miss<=2 else "NO")