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

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

yes = False

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

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

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

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

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

Solution (Python):
import sys
N = int(input())
p_s = list(map(int,input().split()))
cnt = 0
for i,p in enumerate(p_s):
    if i+1 != p:
  	    cnt += 1
    if cnt > 2:
        break
else:
    print("YES")
    sys.exit(0)
print("NO")

Solution (Python):
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7



def check(P):
    t = 0
    for p in P:
        if t > p:
            return False
        t = p
    return True

def main():
    N = i2n()
    P = i2nn()
    if check(P):
        print('YES')
        return
    for i in range(N):
        for j in range(i+1, N):
            P[i], P[j] = P[j], P[i]
            if check(P):
                print('YES')
                return
            P[i], P[j] = P[j], P[i]
    print('NO')

main()

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))
sorted_arr = list(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())
P = list(map(int, input().split()))
A = list(i for i in range(1,N+1))

if P == A:
    print('YES')
    exit()
for i in range(0, N-1):
    for j in range(i+1, N):
        B = A
        B[i], B[j] = B[j], B[i]
        if P == A:
            print('YES')
            exit()
        B[i], B[j] = B[j], B[i]
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())
li = list(map(int, input().split()))

cnt = 0
for i in range(a):

  if li[i] != i + 1:
    cnt += 1
    
if cnt == 0 or cnt == 2:
  print("YES")
else:
  print("NO")

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

hoge=0
for i in range(N):
        if A[i] != C[i]:
                hoge += 1

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

Solution (Python):
import sys

N = int(input())
p = list(map(int, input().split()))
checker = p.pop(0)
l = []
for pp in p:
    if checker > pp:
        l.append(pp)
    checker = pp
    
if len(l) == 0:
    print('YES')
    sys.exit()    
    
if len(l) == 2:
    temp_p = p[:]
    temp_p[p.index(l[0])] = l[1]
    temp_p[p.index(l[1])] = l[0]
    checker = temp_p.pop(0)
    for pp in temp_p:
        if pp < checker:
            print('NO')
            sys.exit()
    print('YES')
else:
    print('NO')

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

flg = None
for i, p in enumerate(P):
    index = i+1
    if index != p:
        if P[p-1] == index and ((not flg) or index==flg):
            flg = p
        else:
            print('NO')
            break
else:
    print('YES')

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

if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    n = int(input())
    pp = list(map(int, input().split()))

    cnt = 0

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

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

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

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

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

for i, v in enumerate(p):
    if i + 1 != v:
        c += 1

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

Solution (Python):
import sys
import copy
def checkshojun(l):
    if all([l[i+1]>l[i] for i in range(len(l)-1)] ):
        return True
    else:return False
    
N = int(input())
P = [int(i) for i in input().split()]
if checkshojun(P):
    print("YES")
    sys.exit()

prev = P[0]
for i in range(1,N):
    if prev > P[i]:
        kouho = i -1  
        break
    prev = P[i]
    if i == N-1:
        print("NO")
        sys.exit()
k = P[kouho]

for t in range(kouho+1,N):
    li = copy.deepcopy(P)
    li[kouho] = li[t]
    li[t] = k
    if checkshojun(li):
        print("YES")
        sys.exit()
print("NO")

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = [int(input_raw[i]) for i in range(N)]
    sortA = sorted(A)

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

if __name__ == '__main__':
    main()

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

Solution (Python):
import itertools
import copy


def is_sorted(arr):
    for i in range(len(arr) - 1):
        if arr[i] > arr[i + 1]:
            return False
    return True


def main():
    n = int(input())
    ps = list(map(int, input().split()))

    if is_sorted(ps):
        print('YES')
        return

    for i, j in itertools.combinations(range(n), 2):
        ps_ = copy.deepcopy(ps)
        ps_[i], ps_[j] = ps_[j], ps_[i]
        if is_sorted(ps_):
            print('YES')
            return
    print('NO')
    return


main()

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

sort_p = sorted(p, key=lambda x: x)

counter = 0
for i in range(len(sort_p)):
    if sort_p[i] != p[i]:
        counter += 1
    if counter > 2:
        print("NO")
        exit()
if counter == 2 or counter == 0:
    print("YES")
else:
    print("NO")

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

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

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

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

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

Solution (Python):

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

p_ = sorted(p)

cnt_ = 0
for i, j in zip(p, p_):
    if i != j:
        cnt_ += 1

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

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

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

cnt = 0

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

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

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

Solution (Python):
import copy
import sys

def isshoujun(ls):
    for i in range(N-1):
        if ls[i] > ls[i+1]:
            return False
    return True

N = int(input())
A = list(map(int,input().split()))
for i in range(N-1):
    for j in range(i, N):
        temp = copy.deepcopy(A)
        x = temp[i]
        temp[i] = temp[j]
        temp[j] = x
        if isshoujun(temp):
            print("YES")
            sys.exit()
print("NO")

Solution (Python):
import sys
import numpy as np

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

p_a = np.arange(N) +1


cnt = 0
for i in range(N):
    if p_in[i] == p_a[i]:
        continue
    else:
        cnt += 1


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

Solution (Python):
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
gi = lambda : list(map(int,input().strip().split()))
n, = gi()
l = gi()
ll = sorted(l)
diff = sum([1 if l[k] != ll[k] else 0 for k in range(n)])
if diff <= 2:
	print("YES")
else:
	print("NO")

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
mistakelist = []
for i in range (len(p)):
  if p[i] == i+1:
    continue
  else:
    mistakelist.append(i+1)
if len(mistakelist) ==0:
  print ("YES")
elif len(mistakelist) == 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=count+1
  
if count<=2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
ls = list(map(int, input().split()))
ls_sort = sorted(ls)
tmp = 0

for i in range(n):
    if ls[i] != ls_sort[i]:
        tmp += 1
if tmp == 2 or tmp == 0:
    print("YES")
else:
    print("NO")

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

a2 = a1[:]
a2.sort()

diff = 0
for i in range(n):
    if a1[i] != a2[i]:
        diff += 1
print('YES' if diff == 0 or diff == 2 else 'NO')

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

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

corrects = sorted(p, reverse=False)
count = 0
for i in range(N):
    if p[i] != corrects[i]:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import numpy as np


def main():
    n = int(input())
    p = list(map(int,input().split()))
    a = np.array(list(range(1,n+1)))
    p = np.array(p)
    ans = len(np.where((a-p)!=0)[0])

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






main()

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

Solution (Python):


def resolve():
    N = int(input())
    p = list(map(int, input().split()))
    a = []
    for i in range(N):
        if p[i] != i+1:
            a.append(i)

    if len(a) == 0:
        print('YES')
        return

    if len(a) != 2:
        print('NO')
        return

    p[a[0]], p[a[1]] = p[a[1]], p[a[0]]

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

    print('YES')



if __name__ == "__main__":
    resolve()

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
t = readl()
k = list(t)
k.sort()

flg = False
for i in range(n):
    if t[i] != k[i]:
        if flg == False:
            flg = True
            for j in range(i, n):
                if t[j] == k[i]:
                    w = t[j]
                    t[j] = t[i]
                    t[i] = t[j]
        else:
            print("NO")
            exit()
print("YES")

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
d = []
for i in range(n):
    if p[i] != q[i]:
        d.append((p[i], q[i]))

if len(d) == 0:
    print('YES')
elif len(d) == 2 and d[0][0] == d[1][1] and d[0][1] == d[1][0]:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
ilist = list(map(int, input().split()))
cnt = 0

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

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

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

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

def main():
    N = int(input())
    P = [int(p) for p in input().split()]
    P_ans = [i+1 for i in range(N)]
    
    ans = "NO"
    
    if P == P_ans:
        ans = "YES"
    
    for i in range(N-1):
        for j in range(i+1,N):
            P_swap = [p for p in P]
            P_swap[i],P_swap[j] = P_swap[j],P_swap[i]
            if P_ans == P_swap:
                ans = "YES" 
                break
            
    print(ans)
        
    
    
if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():


    N = II()
    P = LMIIS()

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




    pass
if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import copy


def is_sorted(arr):
    for i in range(len(arr) - 1):
        if arr[i] > arr[i + 1]:
            return False
    return True


def main():
    n = int(input())
    ps = list(map(int, input().split()))

    if is_sorted(ps):
        print('YES')
        return

    for i, j in itertools.combinations(range(n), 2):
        ps_ = copy.deepcopy(ps)
        ps_[i], ps_[j] = ps_[j], ps_[i]
        if is_sorted(ps_):
            print('YES')
            return
    print('NO')
    return


main()

Solution (Python):
n=int(input())
p =[int(i) for i in input().split()]
q =[i for i in range(1,n+1)]
boo=[(p[i]==q[i]) for i in range(n)]
if n-sum(boo)==2 or n-sum(boo)==0: print("YES")
else:print("NO")

Solution (Python):

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

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

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

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

ans = False
for i in range(N-1):
    for j in range(i,N):
        q = p.copy()
        q[i],q[j] = q[j],q[i]

        for n in range(1,N):
            if q[n-1]>=q[n]:
                break
        else:
            ans = True

print('YES' if ans else 'NO')

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

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

Q = sorted(P)

if Q == P:
    print('YES')
    exit()

for i in range(N):
    for j in range(i + 1, N):
        R = copy.deepcopy(P)
        R[i], R[j] = R[j], R[i]
        if R == Q:
            print('YES')
            exit()
print('NO')

Solution (Python):
N = int(input())
pn = list(map(int,input().split()))
pn_sort = sorted(pn)

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

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

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

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

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input().strip())
ps = list(map(int, input().strip().split(' ')))

sorted_list = sorted(ps)

if ps == sorted_list:
    print('YES')
    sys.exit(0)

for i in range(0, N - 1):
    for j in range(i + 1, N):
        mirror = ps[:]
        mirror[i], mirror[j] = mirror[j], mirror[i]
        if mirror == sorted_list:
            print('YES')
            sys.exit(0)
print('NO')

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

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

cnt=0
pal=0

for num in A:
    pal+=1
    if pal != num:
        cnt+=1

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

Solution (Python):
def main():
    n = int(input())
    ps = input().split(' ')
    base = [str(i) for i in range(1, n+1)]
    count = 0
    for i in range(0, n):
        if base[i] != ps[i]:
            count += 1
    if count == 2 or count == 0:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

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

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

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

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

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


def check(ls):
        for i in range(len(ls)-1):
                if ls[i] > ls[i+1]:
                        return False
        return True

for i in range(n):
        for j in range(i,n):
                p[i],p[j] = p[j],p[i]
                if check(p):
                        print('YES')
                        sys.exit()
                p[j],p[i] = p[i],p[j]
print('NO')

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

    
if __name__ == '__main__':
    main()

Solution (Python):
def isSorted(x):
  return x == sorted(x)

n = int(raw_input())
a = map(int,raw_input().split())
flag = isSorted(a)
for i in range(n):
  if flag : break
  for j in range(i):
    t = a[i]
    a[i] = a[j]
    a[j] = t
    if(isSorted(a)==True):
       flag = True
    t = a[i]
    a[i] = a[j]
    a[j] = t
if flag:
  print "YES"
else:
  print "NO"

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

s = 0
for p, q in zip(P, list(range(1,51))):
  if p!=q:
    s+=1

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

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  

def solve(N: int, p: "List[int]"):
    count = 0
    for i, x in enumerate(p):
        if i + 1 != x:
            count += 1
    if count <= 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):
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)

def main():
    N = int(input())
    P = [int(c) for c in input().split()]
    sorted_P = sorted(P)
    if sorted_P == P:
        print("YES")
        sys.exit()
    for i in range(N):
        for j in range(N):
            copied_P = P.copy()
            if i == j:
                continue
            tmp = copied_P[i]
            copied_P[i] = copied_P[j]
            copied_P[j] = tmp
            if sorted_P == copied_P:
                print("YES")
                sys.exit()
                break
            else:
                continue
    else:
        print("NO")
            


if __name__ == '__main__':
    main()

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

Solution (Python):
def is_sorted(lst):
    for i in range(len(lst) - 1):
        if lst[i] > lst[i + 1]:
            return False
    return True

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

yes  = False
for i in range(N):
    for j in range(i, N):
        tmp = p[j]
        p[j] = p[i]
        p[i] = tmp
        if is_sorted(p):
            yes = True
            break
        tmp = p[j]
        p[j] = p[i]
        p[i] = tmp
    if yes:
        break

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

Solution (Python):


from itertools import combinations
from collections import defaultdict
from pprint import pprint
import math
import datetime
import sys

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

sortedP = sorted(P)

diff = 0
for p1, p2 in zip(P, sortedP):
    if p1 != p2:
        diff += 1

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

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

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

tmp = 0

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

N = INT()
p = LIST()

fail = 0

for i, pi in enumerate(p):
    if pi != i + 1:
        fail += 1

if fail in [0, 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 >= 3:
        print("NO")
        break
else:
    print('YES')

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
P=LIST()

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

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

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

    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 = sorted(p)

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

print('YES')

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
print('YES' if check == N or check == N - 2 else 'NO')

Solution (Python):
n = int(raw_input())
nums = map(int, raw_input().split(' '))
corrects = [i for i in range(1, n+1)]
diffs = sum([c != num for c, num in zip(nums, corrects)])
print 'YES' if diffs <= 2 else '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 >= 3:
    answer ="NO"
else:
    answer = "YES"
print(answer)

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

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
def f(a):
    return all([ a[i] < a[i+1] for i in range(N-1)])
for i in range(N):
    for j in range(i+1, N):
        ls = [x for x in p]
        tmp = ls[i]
        ls[i] = ls[j]
        ls[j] = tmp
        if f(ls):
            print("YES")
            exit(0)
if f(p):
    print("YES")
else:
    print("NO")

Solution (Python):

"""
AtCoder A
"""

import sys, math, random
import numpy as np








inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    P = list(map(int,input().split()))
    Pl=P.copy()
    P.sort()
    miss=0
    for i in range(N):
        
        if P[i]!=Pl[i]:
            miss+=1
    
    if miss==2 or miss==0:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

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

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

Solution (Python):
import sys


def input():
    return sys.stdin.readline()[:-1]


def main():
    count = 0
    n = int(input())
    p = [int(i) for i in input().split()]

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

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


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]
a = sorted(p)
from itertools import combinations

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

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

print('NO')

Solution (Python):
N = int(input())
A = []
num = 0
for Ind, p in enumerate(input().split()):
	
	if int(p) != int(Ind+1):
		num += 1

if num >= 3:
	print("NO")
else:
	print("YES")

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

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

def main():
	n = int(input().strip())
	v = list(map(int, input().strip().split()))
	v2 = sorted(v)
	if v == v2:
		print('YES')
		return
	for i in range(n-1):
		for j in range(i+1, n):
			w = v[:]
			w[i], w[j] = w[j], w[i]
			if w == v2:
				print('YES')
				return
	print('NO')

if __name__ == '__main__':
	main()

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

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

Solution (Python):


''' 
------------------------
author : iiou16
------------------------
'''

def main():
    N = input()
    P = list(map(int, input().split()))
    P_s = sorted(P)

    flag = 0
    for i in range(len(P)):
        if P[i] != P_s[i]:
            flag += 1
        if flag > 2:
            print("NO")
            break
    if flag < 3:
        print("YES")

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
P = [int(p) for p in input().split()]
diff = []
for i in range(N):
    if P[i] != i+1: diff.append(i)
if len(diff) > 2: print("NO")
else: print("YES")

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
ans = sum(a != b for a, b in zip(P, sorted(P)))
print("YES" if ans <= 2 else "NO")

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

PS = sorted(P)
M = [i for i in range(N) if P[i] != PS[i]]
if len(M) <= 2 :
    print('YES')
else :
    print('NO')

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

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

Solution (Python):
N = int(input())
*P, = 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(' ')))
sort_P = sorted(P)



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

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

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

swap = 0
result = True
for i in range(1, N + 1):
    if p_list[i - 1] != i:
        swap += 1
    if swap > 2:
        result = False
        break

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

Solution (Python):
from collections import Counter, defaultdict

def main():
    num = int(input())
    data = list(map(int, input().split()))
    ind = 0
    for i in range(num):
        if data[i] != i + 1:
            ind += 1

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







if __name__ == '__main__':
    main()