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):
def main():

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')
        
    
    

    N = int(r())
    P = R()
    tmp = 0
    for i, p in enumerate(P):
        if i+1 != p:
            tmp += 1
        if tmp >= 3:
            print('NO')
            exit()
    print('YES')
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """

if __name__ == '__main__':
    main()

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

p_sorted = sorted(p)

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

Solution (Python):
import sys

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

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

if __name__ == "__main__":
    main()

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

i_tmp = 0
swap_list = []
for i in n_list:
    if i - i_tmp != 1:
        swap_list.append(i)
    i_tmp += 1

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

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

sorted_P = sorted(P, reverse=False)

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

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

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

def shou(a):
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return -1
    return 1

f=0
for i in range(n):
    for j in range(n):
        tmp=a[i]
        a[i]=a[j]
        a[j]=tmp
        if shou(a)==1:
            f=1
        tmp=a[i]
        a[i]=a[j]
        a[j]=tmp
if f>0:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(raw_input())
inp = [int(i) for i in raw_input().split()]

sorted_inp = sorted(inp)

diff = 0

for i in range(len(inp)):
  if inp[i] != sorted_inp[i]:
    diff += 1

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

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

Solution (Python):
import sys
import math

len = int(input())
numList = [int(n) for n in input().split()]

errCount = 0
for i in range(len):
    if numList[i] != int(i+1):
        errCount+=1

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

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

Solution (Python):
import numpy as np

s = input().split()
N=int(s[0])
s = input().split()
a=np.array(range(1,N+1))
s=[int(i) for i in s]
if np.count_nonzero(a != s)<=2:
    print('YES')
else:
    print('NO')

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

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

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

Solution (Python):
def check(a):
  for i in range(1,len(a)):
    if a[i-1]>a[i]:
      return False
  return True
n=int(input())
a=list(map(int,input().split()))
if check(a):
  print('YES')
  exit()
for i in range(n):
  for j in range(n):
    if i==j:
      continue
    a[i],a[j]=a[j],a[i]
    if check(a):
      print('YES')
      exit()
    a[i],a[j]=a[j],a[i]
print('NO')

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

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

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

P2 = P.copy()
P2.sort()
cnt = 0
for i in range(N):
    if P[i]==P2[i]:
        cnt += 1
if cnt==N or cnt==N-2:
    print("YES")
else:
    print("NO")

Solution (Python):





N = int(input())
Pn = list(map(int, input().split()))
ret = 0
for i,p in enumerate(Pn):
  if i+1 != p:
    ret += 1
if ret <= 2:
  print("YES")
else:
  print("NO")

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

Solution (Python):
input();a=[int(j)-i-1for i,j in enumerate(input().split())];print("YES"if a.count(0)>len(a)-3else"NO")

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

relations = {}

for i in range(N):
    relations[i+1] = P[i]

cnt = 0
for key, value in relations.items():
    if key != value:
        cnt += 1

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

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

count = 0
for i, a in enumerate(A, 1):
  if i != a:
    count += 1
    if count > 2:
      print("NO")
      exit()
      
print("YES")

Solution (Python):
num_lines = int(input())
if num_lines<2 or num_lines>50:
	print ('Illegal')
	exit()

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

x=[]

j=0
f=False
for i in range(num_lines):
	if(( i==0 or a[i]>=a[i-1]) and (i==num_lines-1 or a[i]<=a[i+1])):
		continue
	else:
		x.append(i)
		j+=1

if j>0 and j<=4 and j !=1:
	p=a[x[0]]
	a[x[0]]=a[x[j-1]]
	a[x[j-1]]=p
	for k in range(j):
		i=x[k]
		if(( i==0 or a[i]>=a[i-1]) and (i==num_lines-1 or a[i]<=a[i+1])):
			continue
		else:
			f=True
			break
elif j!=0:
	f=True
if f:
	print("NO")
else:
	print("YES")

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

Solution (Python):


n=int(raw_input())
p=map(int,raw_input().split())
cnt=0
for i in xrange(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()))

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):
N = int(input())
p = list(map(int, input().split()))
clist = []
ans = 0

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

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

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

for i in range(N):
  if i+1 != A[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()))

w = []
for i in range(0, len(p)):
  if i+1 != p[i]:
    w.append(i)

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

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

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

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

def main():
	n = int(input().strip())
	v = list(map(int, input().strip().split()))
	w = sorted(v)
	c = 0
	for i in range(n):
		if v[i] != w[i]:
			c += 1
	if c == 0 or c == 2:
		print('YES')
	else:
		print('NO')

if __name__ == '__main__':
	main()

Solution (Python):
num_input = input()
list_input = input().split(" ")
N = int(num_input)
count = 0
for i in range(0,N-1):
  check = int(list_input[i])-(i+1)
  if not check == 0:
    count += 1
    if count == 3:
      break

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

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

for i in range(N):
    if p[i] != sort_p[i]:
        res += 1
        if res > 2:
            print("NO")
            exit()
print("YES")

Solution (Python):
N = int(input())
p_list = list(map(int, input().split()))
slist = sorted(p_list)
if p_list == sorted(p_list):
    print('YES')
else:
    list1 = [1 if p_list[i] == slist[i] else 0 for i in range(N)]
    if list1.count(0) == 2:
        print('YES')
    else:
        print('NO')

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

Solution (Python):
def main():
    _ = int(input())
    P = list(map(int, input().split()))
    c = 0
    for i, p in enumerate(P):
        if i + 1 != p:
            c += 1
    print('YNEOS'[c > 2::2])


main()

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

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

x = int(0)
l = sorted(p)

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

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

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
for i in range(n):
  if(l[i]!=(i+1)):
    ind=l.index(i+1)
    l[i],l[ind]=l[ind],l[i]
    break
count=0

for i in range(n):
  if(l[i]!=(i+1)):
    break
  count+=1
if(count==n):
  print("YES")
else:
  print("NO")

Solution (Python):
_,p=open(0)
i=a=0
for p in p.split():i+=1;a+=int(p)!=i
print('YNEOS'[a>2::2])

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
a.insert(0,0)
f=1
for i in range(1,n+1):
    if a[i]!=i:
        r=a[i]
        t=a[i]
        a[i]=a[r]
        a[r]=t
        break
for i in range(n):
    if a[i]>a[i+1]:
        f=0
if f==0:
    print("NO")
else:
    print("YES")

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

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

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

Solution (Python):
class ABC135:
    def __init__(self):
        pass

    def solve(self, N):
        ans = 0
        p = list(map(int, input().split()))
        ps = sorted(p)
        for i in range(N):
            if(p[i] != ps[i]):
                ans += 1
        if(ans == 0 or ans == 2):
            print("YES")
        else:
            print("NO")

if __name__ == '__main__':
    N = int(input())
    problem = ABC135()
    problem.solve(N)

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

Solution (Python):


i = input()
array = list(map(int, input().split()))

v = 1
counter = 0

for num in array:
    if num != v:
        counter += 1
    v += 1

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

Solution (Python):
n = int(input())
data = input().split(" ")
for i in range (len(data)):
    data[i] = int(data[i])
arr = sorted(data)
ctr = 0
for j in range (len(data)):
    if arr[j] != data[j]:
        ctr += 1
if ctr == 0 or ctr == 2:
    print("YES")
else:
    print("NO")

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

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

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

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

flag = 0

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

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

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

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

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

Solution (Python):
from sys import exit
n = int(input())
ls = list(map(int,input().split()))
if sorted(ls)==ls:
    print("YES")
    exit()
for i in range(len(ls)-1):
    for j in range(i+1,len(ls)):
        l = list(ls)
        l[i],l[j]=l[j],l[i]
        if sorted(l)==l:
            print("YES")
            exit()
print("NO")

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

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

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

Solution (Python):
print("YNEOS"[sum(i!=int(q)for i,q in enumerate(open(0).read().split()))>3::2])

Solution (Python):
print("YNEOS"[sum(i!=int(q)for i,q in enumerate(open(0).read().split()))-1>2::2])

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

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

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
s=sorted(a)
for i in range(0,n):
  for j in range(i,n):
    b=a[:]
    b[i],b[j]=b[j],b[i]
    if s==b:
      print("YES")
      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
if cnt==0 or cnt==2:
    print("YES")
else:
    print("NO")

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

Solution (Python):
n = int(input())
swaps = 0
ar = [int(x) for x in input().split()]
for i in range(len(ar)):
    mn = ar[i]
    mi = i
    for j in range(i, len(ar)):
        if mn > ar[j]:
            mn = ar[j]
            mi = j
    if mi != i:
        if swaps == 1:
            print("NO")
            exit(0)
        ar[i], ar[mi] = ar[mi], ar[i]
        swaps += 1
print("YES" if swaps <= 1 else "NO")

Solution (Python):
input();print("YNEOS"[len([1 for i,q in enumerate(input().split(),1)if int(q)!=i])>2::2])

Solution (Python):
input();print("YNEOS"[len([1 for i,q in enumerate(map(int,input().split()),1)if q!=i])>2::2])

Solution (Python):

def main():
    n = int(input())
    P = tuple(map(int, input().rstrip().split()))
    flg = sum(i != j for i, j in enumerate(P, 1)) <= 2
    print("YES" if flg else "NO")


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys

raw_input = sys.stdin.readline

f = lambda : raw_input().rstrip()    
f1 = lambda : f().split()
f2 = lambda t: map(t,f1())

def main1():
    a,b = f2(int)
    if (a+b)%2 == 0:
       print (a+b)/2
    elif a == b:
       print 0
    else:
       print "IMPOSSIBLE"

def main2():
    n = int(f())
    a = f2(int)
    b = sorted(a)
    m = 0
    for i in xrange(n):
        if a[i] != b[i]:
            m = m + 1
    if m<=2:
        print "YES"
    else:
        print "NO"

def main3():
    n = int(f())
    a = f2(int)
    b = f2(int)
    ans = 0
    for k in xrange(n):
        if a[i] >= b[i]:
            ans = ans + b[i]
        elif a[i] < b[i] :
            ans = ans + a[i]
            b[i] = b[i] - a[i]
            ans = ans + min(a[i+1], b[i])
            a[i+1] = a[i+1] - b[i]  
    print ans 
 
def main4():
    pass
    
    
if __name__ == "__main__":
    main2()

Solution (Python):
N = int(input())
X = [int(i) for i in input().split()]
count = 0
for i in range(N):
    if X[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
if cnt<=2:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
import math
INPUT = sys.stdin.readline

def SORO_INT(): return int(INPUT())
def MULT_INT_LIST(): return list(map(int, INPUT().split()))
def MULT_INT_MAP(): return map(int, INPUT().split())
def SORO_STRING(): return INPUT()
def MULT_STRING(): return INPUT().split()

N = SORO_INT()
A = MULT_INT_LIST()
X = sorted(A)
if A == X:
    print('YES')
    sys.exit()


i = 0
count=0
for i in range(N):
    if count>2:
        print('NO')
        sys.exit()
    
    if A[i]==i+1:
        count = count
    else:
        count+=1

print('YES')

Solution (Python):
N=int(input())
p=[int(i) for i in input().split()]
tmp=0
for i in range(N):
    if p[i]!=i+1:
        tmp+=1
if tmp<=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 < 3: print('YES')
else: print('NO')

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

ans = "NO"

if sorted(P) == P:
    ans = "YES"

for i in range(N):
    for j in range(i+1,N):
        tmp = P[:]
        tmp[i],tmp[j] = tmp[j],tmp[i]
        if sorted(tmp) == tmp:
            ans = "YES"
            break

print(ans)

Solution (Python):
n = int(input())
p = [ int(_) for _ in input().split() ]
ls = [ _ for _ in range(1, n+1) ]
cnt = int()

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

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

c = 0
for i, v in zip(range(N), p):
    if i+1 != v:
        c+=1

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

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

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

Solution (Python):



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

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

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

p_collect = sorted(p)

no = 0

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

if no == 1:
    print("NO")
elif no >= 3:
    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("NO")
else:
    print("YES")

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

permulated = len([1 for k, p in enumerate(P, 1) if p != k])
print('YES' if permulated <= 2 else 'NO')

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

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

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

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

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

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

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
p = wi()
q = sorted(p)

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

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

Solution (Python):
n = int(input())
l = [int(x) for x in input().split()]
s_l = sorted(l)
print("YES" if sum([1 if l[i]!=s_l[i] else 0 for i in range(n)]) <= 2 else "NO")

Solution (Python):
N, *P = list(map(int, open(0).read().split()))
count = sum(p != i + 1 for i, p in enumerate(P))

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

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

Solution (Python):
_,p=open(0);print('YNEOS'[sum(i!=int(p)for i,p in enumerate(p.split(),1))>2::2])

Solution (Python):
_,p=open(0);print('YNEOS'[sum(i!=int(p)for i,p in enumerate(p.split(),1))>2::2])

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

q = sorted(P)

if sum(p_i!=q_i for p_i, q_i in zip(P, q)) <= 2:
    print("YES")
else:
    print("NO")

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

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

Solution (Python):

import math

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

r = []

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

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