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()))
ln=[i for i in range(1,n+1)]
f=0
for i in range(n):
	if p[i]!=ln[i]:
		f+=1
if f==2 or f==0:
	print('YES')
else:
	print('NO')

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
r=list(range(1,n+1))
s=0
m=0
if l==r:
  m=1
  print("YES")
else:
  while s<n:
    for i in range(n):
      r=list(range(1,n+1))
      d=r[s]
      r[s]=r[i]
      r[i]=d
      if l==r:
        print("YES")
        s=n
        m=1
        break
    s=s+1
if not m==1:
  print("NO")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
ideal_list = [i for i in range(1, N+1)]
delta_list = [abs(a-b) for a, b in zip(p, ideal_list)]
if len([x for x in delta_list if x != 0]) in [0, 2]:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
pn = [ int(item) for item in input().split()]
ANS = list(range(1,N+1))

import copy

if pn == ANS:
  print('YES')
else:
  flag=False
  for i in range(len(pn)):
    pi = pn[i]
    for j in range(len(pn)):
      pj = pn[j]
      pnc = copy.deepcopy(pn)
      pnc[j] = pi
      pnc[i] = pj
      if pnc == ANS:
        print('YES')
        flag=True
        break
    if flag==True:
      break
  if flag == False:
    print('NO')

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

out_num = {}
for i,pi in zip(range(1,N+1),p):
  if i != pi:
    out_num[i] = pi

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

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

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

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

Solution (Python):
import queue

"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""

def isUp(A):
	p = 0
	for a in A:
		if a < p:
			return False
		p = a
	return True
N = int(input())
A = [int(i) for i in input().split()]
ans = False

if isUp(A):
	ans = True
if not ans:
	for i in range(N-1):
		for j in range(i+1,N):
			A[i] += A[j]
			A[j] = A[i]-A[j]
			A[i] = A[i]-A[j]
			if isUp(A):
				ans = True
				break
			A[i] += A[j]
			A[j] = A[i]-A[j]
			A[i] = A[i]-A[j]
if ans:
	print("YES")
else:
	print("NO")

Solution (Python):
from copy import deepcopy
from itertools import combinations


N = int(input())
ppp = list(map(int, input().split()))
ppp_sorted = sorted(ppp)

combis = list(combinations(ppp, 2))

if ppp == ppp_sorted:
    print('YES')
    exit()
if N == 2:
    print('YES')
    exit()

for combi in combis:
    i = ppp.index(combi[0])
    j = ppp.index(combi[1])
    ppp_copy = deepcopy(ppp)
    ppp_copy[i], ppp_copy[j] = ppp[j], ppp[i]
    if ppp_copy == ppp_sorted:
        print('YES')
        exit()

print('NO')

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

Solution (Python):
def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]

N = getInt()
p = getIntList()
n = 0
for i in range(N):
    if not (i+1==p[i]):
        n += 1
if(n<=2):
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = 0
b = []
c = [] 

for i in range(len(a)):
    if a[i]!=i+1:
        count = count + 1
        b.append(i+1)
        c.append(a[i])
        
if count == 0:
    print('YES')
elif count == 2 and b[0] == c[1] and c[0] == b[1]:
    print('YES')
else:
    print('NO')

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

c=0
d=0
for i in range(n):
    if p[i]==i+1:
        continue
    else:
        c=c+1
        if c>=3:
            break
        if d==0:
            d=i+1
            continue
        if d!=0:
            if d!=p[i]:
                c=3
                break

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

Solution (Python):
L1 = input()
L2 = list(map(int,input().split()))
L3 = L2.copy()
L3.sort()

cnt = 0

for a ,b in zip(L2 , L3) :
  if a != b:
    cnt += 1

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

Solution (Python):
S = int(input())
slist = input().split()

for i,a in enumerate(slist):
  slist[i] = int(a)

new_list = sorted(slist)
d_count = 0
for i,j in zip(slist,new_list):
  if i != j:
    d_count += 1
    
if d_count <= 2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
p = tuple(map(int,input().split()))
ans = sorted(p)
for i in range(n):
    for j in range(n):
        ori = list(p)
        temp = ori[i]
        ori[i] = ori[j]
        ori[j] = temp
        if ans == ori:
            print("YES")
            exit()
print("NO")

Solution (Python):

def main():

    N = int(input())
    A= list(map(int,input().split()))
    against = list(range(1,N+1))
    count = 0

    for i in range(N-1):
        if A[i]!=against[i]:
            count +=1
    
    if count < 3:
        print("YES")
    else:
        print("NO")




if __name__== "__main__":
    main()

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

dif = []
dif_idx = []
for i in range(N):
    if p[i] != (i+1):
        dif.append(p[i])
        dif_idx.append(i)

flag_able = True
if len(dif) > 2:
    flag_able = False
elif len(dif) == 2:
    pass    

if flag_able is True:
    print('YES')
else:
    print('NO')

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

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

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

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

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

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


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

Solution (Python):
import copy

N = int(input())
P = list(map(int, input().split()))
X = copy.copy(P)

X.sort()
ans = "NO"


for i in range(N):
    for j in range(N):
        Y = copy.copy(P)
        a = copy.copy(P[i])
        b = copy.copy(P[j])
        Y[i] = b
        Y[j] = a
        
        if Y == X:
            ans =  "YES"


print(ans)

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

def main():
  n = int(input())
  input_line = list(map(int, input().split()))
  
  flag = 0
  i = 0
  buf = []
  for x in input_line:
    if len(buf) > 2:
      break
    elif (i + 1) != x:
      buf.append(i)
    i += 1
    
  if input_line == sorted(input_line):
    print("YES")
  elif len(buf) != 2:
    print("NO")
  else:
    first = buf[0]
    second = buf[1]
    s = input_line[first]
    input_line[first] = input_line[second]
    input_line[second] = s
    if input_line == sorted(input_line):
      print("YES")
    else:
      print("NO")


if __name__ == '__main__':
  main()

Solution (Python):
n=int(input())
p=[int(_) for _ in 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())
pList = list(map(int, input().split()))
sortedList = sorted(pList)
diffCount = 0

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

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

Solution (Python):
n = int(input())
t = input()
p = []
for k in range(0,n,1):
  p.append(int(t.split(' ')[k]))
c = 0
for k in range(0,n,1):
  if p[k] == k+1:
    c += 1
if c == n - 2 or c == n:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input());
list = list(map(int,input().split()))
diffcount = 0

for num in range(1,n+1):
    if list[num-1] != num:
        diffcount += 1
if diffcount == 2 or diffcount == 0:
    print("YES")
else:
    print("NO")

Solution (Python):



import sys

if __name__ == '__main__':
    inputN = int(input())
    inputList = list(map(int, input().split()))
    
    flag = False
    before = 0
    for k in range(inputN):
        if before >= inputList[k]:
            flag = True
        before = inputList[k]
    
    
    if not flag:
        sys.stdout.write("YES")
        sys.exit()
    
    
    for i in range(inputN):
        for j in range(inputN):
            if i != j:
                changeList = inputList[:]
                swap = changeList[i]
                changeList[i] = changeList[j]
                changeList[j] = swap
          
                
                flag = False
                before = 0
                for k in range(inputN):
                    if before >= changeList[k]:
                        flag = True
                    before = changeList[k]
          
                
                if not flag:
                    sys.stdout.write("YES")
                    sys.exit()


    sys.stdout.write("NO")

Solution (Python):
N = int(input())
lst = []
lst_2 = []
input_ = input().split()
for i in range(N):
	lst.append(int(input_[i]))
lst_s = sorted(lst)
for i in range(N):
  if lst[i] != lst_s[i]:
    lst_2.append(i)
if len(lst_2) <= 2:
	print("YES")
else:
	print("NO")

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

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

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

Solution (Python):
num = int(input())
if num >= 2:
  input_list = [int(i) for i in input().split()] 
  sorted_list = sorted(input_list)

  count = 0
  for input, sort in zip(input_list, sorted_list):
    if input != sort:
      count += 1

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

Solution (Python):


import sys

sys.setrecursionlimit(10**7)


input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    n, = read_h()
    arr = read_h()

    bad = 0
    for i, p in enumerate(arr):
        if p != i + 1:
            bad += 1
        if bad > 2:
            print('NO')
            return

    print('YES')


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import numpy as np
import itertools
def main():
    input = sys.stdin.readline

    N = int(input())
    P = list(map(int, input().split()))
    
    P = np.array(P)
    
    ans = np.arange(start=1, step=1, stop=N+1)
    
    if np.all(P == ans):
        print('YES')
    else:
        dels_comb = list(itertools.combinations(P,2))
        

        length = len(dels_comb)
        for i in range(length):
            dels = dels_comb [i]
            a, b = dels

            _P = P[(P != a) & (P != b)]
            _ans = ans[(ans != a) & (ans != b)]
            
            
            
            if np.all(_P == _ans):
                if np.where(P == a)[0] == np.where(ans == b)[0]:
                    if np.where(P == b)[0] == np.where(ans == a)[0]:
                        print('YES')
                        break
            if i + 1 == length:
                print('NO')
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
num_list = input().split()
A = [int(s) for s in num_list]
A.sort()
B = []
for i in range(N):
    if A[i] != int(num_list[i]):
        B.append(1)

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

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

Solution (Python):
def swap(a, b):
    tmp = a
    a = b
    b = tmp;
    return a, b

n = int(input())
p = list(map(int, input().split()))
g = p.copy()
q = sorted(p)
flag = 0
for i in range(n):
    for j in range(n):
        p[i], p[j] = swap(p[i], p[j])
        if (p == q):
            flag = 1
        p = g.copy()
if flag == 1:
    print("YES")
else:
    print("NO")

Solution (Python):
import copy

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

sortedps = copy.deepcopy(ps)
sortedps.sort()

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

print('YES')

Solution (Python):
import numpy as np
def acinput():
    return list(map(int,input().split(" ")))


N=int(input())
p=np.array(acinput())

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

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


index = 0
flag = 0
for i in range(n):
    
    if check[i] != i+1:
        flag +=1
        if flag == 2:
            check[index] = check[i]
            check[i] = tmp
            
            
            break
        
        index = i
        tmp = check[index]
        
        



print("YES") if l == check else print("NO")

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

Solution (Python):
n = int(input())
p = map(int, list((input().split())))
count =0
for s,t in enumerate(p):
	if s !=(t-1):
		count+=1
if count == 2 or count == 0:
	print("YES")
else:
	print("NO")

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
A=p.copy()
A.sort()
if p==A:
  print("YES")
else:
  flag=0
  for i in range(N):
    for j in range(i):
      p[i],p[j]=p[j],p[i]
      if p==A:
        flag=1
        break
      else:
        p[i],p[j]=p[j],p[i]
    if flag==1:
      break
  if flag==0:
    print("NO")
  else:
    print("YES")

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

p_sorted = sorted(p)

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

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

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

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

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

pure_arr = []
for i in range(n):
    pure_arr.append(i+1)
count=0
for (num, value) in zip(pure_arr, arr):
    if num!=value:
        count += 1

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

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

Solution (Python):
N = int(input())
plist = [int(i) for i in list(input().split())]
c=1
check=0
for i in plist:
    if i!=c:
        check+=1
    c+=1

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

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


down_i = []
down_count = 0
for i in range(n-1):
    if p[i] > p[i+1]:
        down_i.append([i,p[i],p[i+1]])
        down_count += 1


if down_count == 0:
    print('YES')
else:
    if down_count == 1:
        p[down_i[0][0]] = p[down_i[0][0]+1]
        p[down_i[0][0]+1] = down_i[0][1]

        c = 0
        for i in range(n-1):
            if p[i] > p[i+1]:
                c += 1

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

    elif down_count == 2:
        p[down_i[0][0]] = down_i[1][2]
        p[down_i[1][0]+1] = down_i[0][1]

        

        c = 0
        for i in range(n-1):
            if p[i] > p[i+1]:
                c += 1

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

Solution (Python):

import itertools

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

    compArr = list(range(1, N+1))
    arr = list(range(N))
    combs = list(itertools.combinations(arr, 2))

    ans = 'NO' 
    if p == compArr:
        ans = 'YES'
    for c in combs:
        tmpArr = compArr.copy()
        tmpArr[c[0]], tmpArr[c[1]] = tmpArr[c[1]], tmpArr[c[0]]
        if p == tmpArr:
            ans = 'YES'

    return ans

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

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

c = 0
for i in range(n):
      if p_lis[i]== org[i]:
            pass
      else:
            c += 1
if c >=3:
      print("NO")
else:
      print("YES")

Solution (Python):
n = int(input())
p = [0] + list(map(int, input().split())) + [0]
ans = 'YES'
d = {}
for i in range(n):
    if p[i] != i:
        if p[p[i]] == i:
            d[tuple(sorted([i, p[i]]))] = 0
            continue
        else:
            ans = 'NO'
            break
if len(d) > 1:
    ans = 'NO'
print(ans)

Solution (Python):
import sys
n = int(sys.stdin.readline())
p = [int(i) for i in sys.stdin.readline().split()]

if p == sorted(p):
    print('YES')
elif len(p) == 2:
    print('YES')
else:
    swap_index = []
    for i, v in enumerate(p[:-1]):
        if v > p[i + 1]:
            if len(swap_index) == 0:
                swap_index.append(i)
            else:
                swap_index.append(i + 1)
                break
    if len(swap_index) == 1:
        print('NO')
    else:
        p[swap_index[0]], p[swap_index[1]] = p[swap_index[1]], p[swap_index[0]]
        if p == sorted(p):
            print('YES')
        else:
            print('NO')

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

count = 0
judge = True

for i in range(N-1):
    if (li[i] != ans[i]):
        count = count + 1
        if(count > 2):
            judge = False
            break

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

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

flag = 0
for i,c in enumerate(l):
  if i+1 != c:
    flag += 1

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

Solution (Python):

n = int(input())
p_n_str = input().split()
p_n_int = [int(p) for p  in p_n_str]
flag = 0

for k in range(0, n):

    if p_n_int[k] != k+1:
        
        for i in range(1, n):
            if k+1 == p_n_int[i]:
                flag += 1
                smp = p_n_int[k]
                p_n_int[k] = p_n_int[i]
                p_n_int[i] = smp
                break
        
if flag <= 1:        
    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==0 or d ==2:
    print("YES")
else:
    print("NO")

Solution (Python):

n = int(input())
p = list(map(int,input().split()))
q = sorted(p)
j = 0 
count = 0 
ans = 0
for i in range(len(p)):
    if p[i] != q[i]:
        if count:
            ans = 1
            break
        elif j:
            if p[i] == q[j]:
                count += 1
            else:
                ans = 1
                break
        else:
            j = i
if ans == 1:
    print("NO")
else:
    print("YES")

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
count = 0
index = [0,0]
k = 0

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

if count == 0:
    print('YES')
elif count == 2:
    if (p[index[0]] == index[1] + 1) & (p[index[1]] == index[0] + 1):
        print('YES')
    else:
        print('NO')
else:
    print('NO')

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

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

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

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

ans = 'YES'
judge = False
for i in range(n):
    if (p[i] != i + 1 and judge == False):
        if(p[p[i] - 1] == i + 1):
            judge = True
            a = p[i] - 1
            p[i] = i + 1
            p[a] = a + 1
        else:
            ans = 'NO'
            break
    elif (p[i] != i + 1 and judge == True):
        ans = 'NO'
        break
print(ans)

Solution (Python):


N = input()

n = int(N)

list1 = list(range(n))

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



list2 = []

list2 = input().split()  

list2 = [int(n) for n in list2]



judge = 0

for i in range(n) : 
	if list1[i] != list2[i] :
		judge = judge + 1

if judge <= 2 :
	print('YES')

else :
	print('NO')

Solution (Python):
import sys



def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))
    c = 0
    for i, x in enumerate(A):
        if ( i + 1 != x ):
            c += 1

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

lines = [x.strip() for x in sys.stdin.readlines()]




solve(lines)

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

Solution (Python):
import copy

N = int(input())
a = list(map(int,input().split()))
ans = "NO"
b = copy.deepcopy(a)
if b == sorted(a):
    ans = "YES"
else:
    for i in range(0, N-1):
        if ans == "YES":
            break
        for j in range(i, N):
            b = copy.deepcopy(a)
            temp = b[i]
            b[i] = b[j]
            b[j] = temp
            if b == sorted(a):
                ans = "YES"
                break
print(ans)

Solution (Python):
import copy
n=int(input())
p=[]
ps=[]
cnt=0
p = list(map(int, input().split()))
pn=copy.deepcopy(p)
ps=p
ps.sort()
for i in range(n):
    if ps[i]!=pn[i]:
        cnt=cnt+1
        if cnt==3:
            break
if cnt==0 or cnt==1 or cnt==2:
    print("YES")
else:
    print("NO")

Solution (Python):
import collections
n = int(input())
p = [int(i) for i in input().split()]
ps = sorted(p)
x = [p[i]-ps[i] for i in range(len(p))]
c = collections.Counter(x)
if len(c) == 1:
  print('YES')
elif len(c) == 3 and x.count(0) == n-2:
  print('YES')
else:
  print('NO')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = []

if a != b:
    for i in range(1, n+1):
        if a[i-1] != i:
            c.append(i-1)

        if len(c) == 2:
            break

    if len(c) == 2:
        ch = a[c[0]]
        a[c[0]] = a[c[1]]
        a[c[1]] = ch

    if a == b:
        print('YES')
    else:
        print('NO')
else:
    print('YES')

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
nl = [i for i in range(1, n+1)]
pi = -1
pii = 999
pj = -1
pji = 999
for i in range(n):
    if pi == -1:
        if p[i] != nl[i]:
            pi = p[i]
            pii = i
    else:
        if p[i] != nl[i]:
            pj = p[i]
            pji = i
            break

if pii != 999 and pji != 999:
    p[pji] = pi
    p[pii] = pj

ans = True
for i in range(n):
    if nl[i] != p[i]:
        ans = False
        break

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

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
answer = []
diff = 0
diff_1 = -1
diff_2 = -1

index = 0

for i in p:
    if index == 0:
        index += 1
        continue
    
    if p[index] < p[index-1]:
        if diff >= 2:
            print ("NO")
            exit()
        diff += 1

        if diff == 1:
            diff_1 = index-1
        else:
            diff_2 = index

    index += 1

if diff_1 == -1:
    print("YES")
    exit()

if diff_1 != -1 and diff_2 == -1:
    print("NO")
    exit()

diff_1_1 = p[diff_1]
p[diff_1] = p[diff_2]
p[diff_2] = diff_1_1

index = 0

for i in p:
    if index == 0:
        index += 1
        continue
    
    if p[index] < p[index-1]:
        print ("NO")    
        exit()

    index += 1

print("YES")

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

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

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

print(ans)

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

is_asc = True

for k in range(N-1):
  if p[k] > p[k+1]: is_asc = False

if is_asc == True:
    print('YES')
else:
  for i in range(N):
      for j in range(N):
          if i == j: continue
          is_asc = True
          tmp_p = copy.deepcopy(p)
          tmp = p[i]
          tmp_p[i] = p[j]
          tmp_p[j] = tmp
          for k in range(N-1):
              if tmp_p[k] > tmp_p[k+1]: is_asc = False
          if is_asc == True:
              break
      if is_asc == True:
          break

  if is_asc == True:
      print('YES')
  else:
      print('NO')

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
A = []
for i in range(N-1):
    if P[i]>P[i+1]:
        A.append([(P[i],i),(P[i+1],i+1)])
l = len(A)
if l > 2 or l == 1:
    print("NO")
elif l == 0:
    print("YES")
elif l == 2:
    a = max(A[0])
    b = min(A[1])
    P[a[1]],P[b[1]] = b[0],a[0]
    for j in range(N-1):
        if P[j] > P[j+1]:
            print("NO")
            j = "j"
            break
    if j != "j":
        print("YES")

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

print('YES')

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

flag = []
for i in range(N-1):
  if P[i] > P[i+1]:
    flag.append(i)

if len(flag) > 2:
  print('NO')
if len(flag) == 2:
  A = P[flag[0]]
  B = P[flag[1]+1]
  P[flag[0]] = B
  P[flag[1]+1] = A
  for i in range(N-1):
    if P[i] > P[i+1]:
      print('NO')
      exit()
  print('YES')
  exit()
if len(flag) == 1:
  A = P[flag[0]]
  B = P[flag[0]+1]
  P[flag[0]] = B
  P[flag[0]+1] = A
  for i in range(N-1):
    if P[i] > P[i+1]:
      print('NO')
      exit()
  print('YES')
  exit()
if len(flag) == 0:
  print('YES')

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

Solution (Python):
import numpy as np

N = int(input())
p = [int(i) for i in input().split()]
q = [i for i in range(1, N+1)]
p = np.array(p)
q = np.array(q)
r = np.array(p-q)
num =  len(np.where(r==0)[0])
print("YES" if N - num == 2 or N == num else "NO")

Solution (Python):

n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i + 1 == p[i]:
        continue
    else:
        ans += 1
        if ans > 2:
            break
if ans <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
list1 = list(map(int, input().split()))
count = 0
 
for i in range(N-1):
  if list1[i] > list1[i+1]:
    count += 1

if count == 0:
  print("YES")
elif count == 1:
  count = 0
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      x = i
  a = list1[x] 
  b = list1[x + 1]
  list1[x] = b
  list1[x + 1] = a
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      count += 1
  if count == 0:
    print("YES")
  else:
    print("NO")
elif count >= 3:
  print("NO")
else:
  count = 0
  list2 = []
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      list2.append(i)
  a = list1[list2[0]] 
  b = list1[list2[1] + 1]
  list1[list2[0]] =b
  list1[list2[1] + 1] =a
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      count += 1
  if count == 0:
    print("YES")
  else:
    print("NO")

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

cnt = 0

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


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

Solution (Python):
import itertools
N = int(input())
A = [int(x) for x in input().split()]
B = list(range(1,N+1))

def test(i,j):
  AA = A.copy()
  AA[i], AA[j] = AA[j], AA[i]
  return AA == B

bl = (A == B)
for i,j in itertools.combinations(range(N),2):
  if test(i,j):
    bl = True
answer = 'YES' if bl else 'NO'
print(answer)

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

index_list = []
count = 0
for i in range(n-1):
    if p[i] >= p[i+1]:
        if count == 0:
            index_list.append(i)
            count += 1
        elif count == 1:
            index_list.append(i+1)
            count += 1
        else:
            break

if count == 0:
    print("YES")
elif count == 2:
    tmp = p[index_list[0]]
    p[index_list[0]] = p[index_list[1]]
    p[index_list[1]] = tmp

    count = 0
    for i in range(n-1):
        if p[i] >= p[i+1]:
            print("NO")
            count += 1
            break
        else:
            pp = p[i]
    if count == 0:
        print("YES")
else:
    print("NO")

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

if ans != "YES":
    for i in range(N-1):
        for j in range(i+1,N):
            pcopy = p.copy()
            pcopy[i],pcopy[j] = pcopy[j],pcopy[i]
            flg = True
            for k in range(N-1):
                if pcopy[k] > pcopy[k+1]:
                    flg = False
            if flg:
                ans ="YES"
                break        

print(ans)

Solution (Python):
import copy
def ls_to_int (ls):
	ls_int = []
	for i in ls:
		ls_int.append(int(i))
	return ls_int

def judge(ls):
	is_sorted = True
	for i in range(len(ls)):
		for j in range(i, len(ls)):
			if ls[i] > ls[j] :
				is_sorted = False
				break
	return is_sorted

N = int(input())
p = input().split()
p = ls_to_int(p)
memory = copy.deepcopy(p)
is_able = False
for i in range(len(p)):
	for j in range(len(p)):
		temp = p[i]
		p[i] = p[j]
		p[j] = temp
		if judge(p):
			is_able = True
			break
		p = copy.deepcopy(memory)
	if is_able:
		break
if is_able:
	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')