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):

"""
Created on Fri May 15 00:38:52 2020

@author: Kanaru Sato
"""

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

yesflag = 1
a = [p[i] for i in range(n)]
for k in range(n-1):
    if a[k] > a[k+1]:
        yesflag = 0
        break

if yesflag == 0:
    for i in range(n-1):
        for j in range(i+1,n):
            a = [p[i] for i in range(n)]
            tmp = a[i]
            a[i] = a[j]
            a[j] = tmp
            yesflag = 1
            for k in range(n-1):
                if a[k] > a[k+1]:
                    yesflag = 0
                    break
            if yesflag == 1:
                break
        if yesflag == 1:
            break

if yesflag == 1:
    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())
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):
import itertools
import math
import fractions
import functools
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=[int(x) for x in input().split()]

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

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

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

Solution (Python):
N = int(input().rstrip())
l1 = [int(i) for i in input().rstrip().split()]
l2 = sorted(l1)
result = 'YES'
dnum = 0
for i in range(N):
    if l1[i] != l2[i]:
        dnum = dnum + 1

    if dnum > 2:
        result = 'NO'
        break

print(result)

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

Solution (Python):
n=int(input());lista_1=[];contador=0;index=0
lista=list(map(int,input().split()))
for i in range(1,n+1):
    lista_1.append(i)
for j in lista:
    aaa=lista_1[index]
    if(j!=aaa):
        contador=contador+1
    index=index+1
if(contador<=2):
    print('YES')
else:
    print('NO')

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

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

Solution (Python):
import sys
import math
import bisect

def solve(A):
    n = len(A)
    B = []
    for i in range(1, n + 1):
        B.append(i)
    if A == B:
        return True
    for i in range(n):
        for j in range(i + 1, n):
            A[i], A[j] = A[j], A[i]
            if A == B:
                return True
            A[i], A[j] = A[j], A[i]
    return False

def main():
    n = int(input())
    A = list(map(int, input().split()))
    if solve(A):
        print('YES')
    else:
        print('NO')

if __name__ == "__main__":
    main()

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<=2 else 'NO')

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)




N = inputI()
P = inputIL()

memo = []
for i in range(1,N+1):
  if P[i-1] != i:
    memo.append((P[i-1],i))
    
if len(memo) == 0:
  print("YES")
  exit()
if len(memo) == 2:
  if memo[0][0] == memo[1][1]:
    print("YES")
    exit()
print("NO")

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

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())
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 = 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):
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque



def input1():
	return int(input())


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets


def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	
	return fact(x+y)/fact(x)/fact(y)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True

n=input1()
p=input_array()
count=0
for i in range(n):
	if i+1!=p[i]:
		count+=1
	if count>2:
		print("NO")
		break
if count==2 or count==0:
	print("YES")

Solution (Python):
def solve(n, ls):
    cnt = 0
    for i in range(n):
        if ls[i] != i + 1:
            cnt += 1
        if cnt > 2:
            return False
    return True


n = int(input())
ls = list(map(int, input().split()))
if solve(n,ls):
    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):


from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    n=ii()
    p = li()
    cnt = 0
    for i in range(1,n+1):
        if i!=p[i-1]:
            cnt+=1
    if cnt<=2:
        print('YES')
    else:
        print('NO')



main()

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

Solution (Python):
import numpy as np
n = int(input())
p = list(map(int, input().split()))
a = np.arange(1, n+1)
p = list(p)
p = np.array(p)
match = a == p
ans = 'YES'
if sum(match) == n - 2:
    pos = np.where(match == False)
    pos1 = pos[0][0]
    pos2 = pos[0][1]
    p[pos1], p[pos2] = p[pos2], p[pos1]
    if sum(a == p) != n:
        ans = 'NO'
elif sum(match) != n:
    ans = 'NO'
print(ans)

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
for i in range(n):
    if p[i] != i+1:
        x = p.index(i+1)
        p[i], p[x] = p[x], p[i]
        break
ok = True
for i in range(n):
    if p[i] == i+1:
        continue
    ok = False
    break
if ok:
    print('YES')
else:
    print('NO')

Solution (Python):

import sys
input = sys.stdin.readline
N = int(input())
p = list(map(int, input().split()))
p_sort =sorted(p)
"""
max_p =max(p)
max_index =p.index(max_p)
min_p =min(p)
min_index =p.index(min_p)


if max_index != N and min_index !=0:
p[max_index] =min_p
p[min_index] =max_p
"""

count =0
ans ="YES"
for i in range(N):
    if p[i] !=p_sort[i]:
        count +=1
        if 2 < count:
            ans ="NO"

print(ans)

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

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

for i in range(N-1):
  if A[i] == i+1:
    pass
  else:
    cnt += 1
    
if cnt <= 2:
  print('YES')
else:
  print('NO')

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

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

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

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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce


import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
P = LI()
T = sorted(P)
for i in range(n):
    for j in range(n):
        tmp = deepcopy(P)
        tmp[i], tmp[j] = tmp[j], tmp[i]
        if T == tmp:
            print("YES")
            exit()

print("NO")

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

Solution (Python):
import numpy as np
print("YES" if (np.arange(1, int(input()) + 1, dtype=np.int8) != np.array(input().split(), dtype=np.int8)).sum() in [0, 2] else "NO")

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

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

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

Solution (Python):
n=int(input())
ps=list(map(int,input().split()))
c=0
for i,p in enumerate(ps, start=1):
    if not p==i:
        c+=1
print('YES' if c<3 else 'NO')

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

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

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

k = s.copy()

k.sort(reverse = False)

t = 0

for i in range(n):
    if s[i] != k[i]:
        t += 1
        
if t<3:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
import copy
N = int(input())
p = [int(x) for x in input().split()]
comp = [i for i in range(1, N+1)]

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

for i in range(N-1):
    for j in range(i+1, N):
        tmp = copy.copy(p)
        x = tmp[i]
        tmp[i] = tmp[j]
        tmp[j] = x
        if tmp==comp:
            print("YES")
            sys.exit()

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

Solution (Python):
def solve():
    N = int(input())
    P = list(map(int, input().split()))
    bp = [False]*N
    for i in range(N):
        if P[i] == i+1:
            bp[i] = True
    counter = 0
    for b in bp:
        if not b:
            counter += 1
    if counter > 2:
        print('NO')
    else:
        print('YES')


solve()

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

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

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

Solution (Python):


N = int(input())
P = [int(p) for p in input().split()]
cnt = 0
i = 1
for j in range(N):
    if i != P[j]:
        cnt += 1
    i += 1
print('YES' if cnt <= 2 else 'NO')

Solution (Python):
n = input()
a = tuple(map(int, input().split()))
p = sorted(a)
ans = 0
for x, y in zip(a, p):
    if x != y:
        ans += 1
if ans == 0 or ans == 2:
    print("YES")
else:
    print("NO")
from sys import argv
debug = 'a' in argv
if debug:
    print(ans)

Solution (Python):
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(input())
P = list(map(int, sys.stdin.readline().rsplit()))

def ok():
    for i in range(N - 1):
        if P[i + 1] < P[i]:
            return False
    return True

if ok():
    print("YES")
else:
    for i in range(N - 1):
        for j in range(i + 1, N):
            P[i], P[j] = P[j], P[i]
            if ok():
                print("YES")
                exit()
            else:
                P[i], P[j] = P[j], P[i]

    print("NO")

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
P = sorted(p)
cnt = 0
for i in range(len(p)):
    if P[i] != 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() ) )

P_ = sorted( P )

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

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

Solution (Python):
def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    count = 0
    for i, v in enumerate(p):
        if i + 1 != v:
            count += 1
    if count == 0 or count == 2:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

Solution (Python):
import sys
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]

N = ri()
p = ri_()
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 = input()
p = list(map(int, input().split()))
sp = sorted(p)
swaped = 0
for i in range(len(p)):
  if p[i] != sp[i]:
    swaped += 1
    
if swaped <= 2:
  print('YES')
else:
  print('NO')

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

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

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

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

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

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

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

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

num = 0
for i, P in enumerate(Ps, start=1):
    if i != P:
        num += 1

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

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

print('YES' if sum(map(lambda x: x[0] == x[1], zip(p, sorted(p)))) >= N - 2 else 'NO')

Solution (Python):
n = int(input())
li1 = list(map(int,input().split()))
li2 = []
count = 0
for i in range(n):
    li2.append(i+1)
for i in range(n):
    if li1[i] != li2[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()))

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

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

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

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

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

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

pp = np.array(sorted(p))-p
sum_=0
for i in pp:
  if i!=0: sum_+=1
if sum_>2: print('NO')
else: print('YES')

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

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

Solution (Python):
n = int(input())
l = list(int(i) for i in input().split())
s = l.copy()
s.sort()

flag = False
if s == l:
  flag = True
else:
  for i in range(n):
    for j in range(i+1, n):
      t = l.copy()
      t[i], t[j] = t[j], t[i]
      if s == t:
        flag = True

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
p = list(map(int, readline().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 = sorted(p)
count = 0
for i in range(n):
    if p[i] != ans[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()))
li = []
cnt = 0
for i in range(n):
    li.append(i+1)
    if li[i] != p[i]:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):

N = int(input())
P= [ int(a) for a in (input().split())]
for i in range(len(P)):
    for j in range(len(P)):
        P_tmp=P.copy()
        if i!=j:
            P_tmp[j]=P[i]
            P_tmp[i]=P[j]
        if P_tmp ==sorted(P):
            print("YES")
            exit()
            break
    
print('NO')

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

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

Solution (Python):
import sys
N = int(input())
A = [int(x) for x in input().split()]
count=0
for n in range(N):
  if A[n]!=n+1:
    count+=1
    if count>2:
      print("NO")
      sys.exit()
if count==1:
  print("NO")
else:
  print("YES")

Solution (Python):
import numpy as np
n = int(input())
s = list(map(int,input().split()))
a = np.array(list(range(1,n+1)))
b = np.array(s)
if len(a[a==b]) >= n-2:
    print("YES")
else:
    print("NO")

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

N = int(input())
p = inputlist()
pa = p[:]
pa.sort()
count = 0
for i in range(N):
    if p[i] != pa[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()))
count=0
for i in range(n):
    if p[i]!=i+1:
        count+=1
if count==0 or count==2:
    print("YES")
else:
    print("NO")

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

count = 0
for i in range(1, N+1):
    if A[i-1] != 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)
cnt = 0
for i in range(n):
    if p[i] != i+1:
        cnt += 1

    


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

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

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

Solution (Python):


def main():
    *N, = map(int, open(0).read().split())
    p = N[1:]
    N = N[0]

    I = []
    for i in range(1, N+1):
        I.append(i)
    
    err = 0
    for i in range(0, N):
        if not p[i] == I[i]:
            err += 1

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


main()

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

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

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

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

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 = list(map(int, input(). split()))
Q = sorted(P)
cnt = 0
i = 0

while cnt <= 2 and i < len(P):
  if Q[i] != P[i]:
    cnt += 1
  i += 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

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

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
k=0
a=[0]*n
for i in range(n):
  a[i]=i+1
  
for i in range(n):
  if a[i]!=p[i]:
    k+=1
    
    
if k<=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 <=  2 else 'NO')

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

cnt = 0

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

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

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 







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 else print('NO')

Solution (Python):
n = int(input())
s = input().split()
a = 0
c = 0
while a < n:
  if int(s[a]) != a+1:
    c += 1
  a += 1
if c == 0 or c == 2:
  print('YES')
else:
  print('NO')

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

Solution (Python):
def resolve():
    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")
resolve()

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


main()

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

count = 0
for i, m in enumerate(M, 1):
    if i != m:
        count += 1

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

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()))

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

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

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

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

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

Solution (Python):
import copy
n = int(input())
p = list(map(int, input().split()))
copy_p = copy.deepcopy(p)
copy_p.sort()
cnt = 0
f = True
for i in range(len(p)):
    if copy_p[i] != p[i]:
        cnt += 1
        f = False

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

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

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

T=sorted(S)

count=0
for i in range (len(S)):
  if S[i]!=T[i]:
    count+=1
if count<=2:
  print("YES")
else:
  print("NO")

Solution (Python):
N = int(input())
P = [int(p) for p in input().split()]
T = sorted(P)
if P == T:
  print('YES')
  exit() 
for i in range(N):
  for j in range(i,N):
    P[i], P[j] = P[j], P[i]
    if P == T:
      print('YES')
      exit()
    else:
      P[i], P[j] = P[j], P[i]
print('NO')

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

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

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

if cnt == 2 or cnt == 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:
    rem = a[i]
    if rem == a[i]:
      count += 1
if count <= 2:
  print("YES")
else:
  print("NO")