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())
a=list(map(int,input().split()))
tmp=sorted(a)
total=0
for i in range(N):
    if a[i]!=tmp[i]:
        total+=1
if total<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 (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()))
count = 0
for i in range(N):
    if p[i] == i+1:
        pass
    else:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

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

q = sorted(p)

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

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

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
q = sorted(p)
cnt = 0
for i in range(n):
    if p[i] != q[i]:
        cnt += 1

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

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

cnt = 0
for p,q in zip(P,Q):
    if p!=q:
        cnt += 1

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

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
p = list(li())

cnt = 0

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

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

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  

def solve(N: int, p: "List[int]"):
    counter = 0
    for i in range(N):
        if (i+1) != p[i]:
            counter += 1
    if counter == 2 or counter == 0:
        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):
n = int(input())
p = list(map(int, input().split()))

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

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

Solution (Python):

import sys
sys.setrecursionlimit(300000)

YES = "YES"  
NO = "NO"  

def solve(N: int, p: "List[int]"):
    s = list(range(1, N + 1))
    cnt = 0
    for i in range(N):
        if s[i] != p[i]:
            cnt += 1
    if cnt == 0 or cnt == 2:
        ret = YES
    else:
        ret = NO

    print(ret)
    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 sys
input=sys.stdin.readline
n=int(input())
p_list=list(map(int,input().split()))
count=0
for i in range(n):
    if p_list[i]!=i+1:
        count+=1
if count==0 or count==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 += 1

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

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

if sum(pi != i + 1 for i, pi in enumerate(p)) <= 2:
    print("YES")
else:
    print("NO")

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

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

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

check = list(range(1, N + 1))
for p, c in zip(P, check):
    diff += (p != c)

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

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

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

Solution (Python):
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op



inf = float('inf')
input = stdin.readline


def main():
    n = get_int()
    P = get_li()
    Q = list(range(1, n+1))
    diff = 0
    for p, q in zip(P, Q):
        if p != q:
            diff += 1
    if diff == 0 or diff == 2:
        print('YES')
    else:
        print('NO')
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

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

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

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

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

if count>2 or count%2==1:
    print("NO")
else:
    print("YES")

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


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

    s = sorted(p)

    print('YES' if sum(i != j for i, j in zip(p, s)) < 3 else 'NO')




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
wrong = 0
for i, a in enumerate(A, 1):
    if i == a:
        continue
    else:
        wrong += 1
if wrong == 2 or wrong == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
ar=list(map(int,input().split()))
def srt(ar):
    return ar==sorted(ar)
b=0
if(srt(ar)):b=1
for i in range(n):
    for j in range(i,n):
        ar[i],ar[j]=ar[j],ar[i]
        if(srt(ar)):b=1
        ar[i],ar[j]=ar[j],ar[i]
if(b):
    print("YES")
else:
    print("NO")

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

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

q = sorted(p)

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

for i in range(n):
    for j in range(i+1,n):
        r = [x for x in p]
        r[i]=p[j]
        r[j]=p[i]
        if q==r:
            print('YES')
            exit()
print('NO')

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

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

Q=sorted(P)

import copy

for i in range(N):
    for j in range(i,N):
        R=copy.deepcopy(P)

        R[i],R[j]=R[j],R[i]

        if Q==R:
            print("YES")
            sys.exit()

print("NO")

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

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

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
X = sorted(P)
num = 0
for x, p in zip(X, P):
    if x != p:
        num += 1
if num == 0 or num == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import numpy as np

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

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

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

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

count = 0
for i, n in enumerate(num):
    if n != i+1:
        count += 1

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

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

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


print("YES" if diff == 0 or diff == 2 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]:
        cnt += 1

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

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

count = 0
for a, b in zip((sorted(inputs)), inputs):
    if a != b:
        count += 1
print('YES' if count == 0 or count == 2 else 'NO')

Solution (Python):



import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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):
a = int(input())
b = list(map(int, input().split()))
c = sorted(b.copy())
d = 0
for i in range(len(b)):
    if c[i] != b[i]:
        d += 1
if d <= 2:
    print("YES")
else:
    print("NO")

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

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

Solution (Python):
import math
N = int(input())
As =   list(map(int,input().split(" ")))

Asorted = As[:]
Asorted.sort()
diff = 0
for i in range(N):
    if Asorted[i] != As[i]:
        diff += 1
if diff == 0 or diff == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p_list = [int(elem) for elem in input().split()]
print("YES" if sum(i != p for i, p in enumerate(p_list, start=1)) <= 2 else "NO")

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

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

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

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

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

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

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

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

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

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

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
B = sorted(P)
for i in range(N):
    for j in range(N):
        P[i], P[j] = P[j], P[i]
        if P == B:
            print('YES')
            exit()
        P[i], P[j] = P[j], P[i]

print('NO')

Solution (Python):



import itertools as ite
import math
import random
import sys
import codecs
from collections import defaultdict

INF = 10 ** 18
MOD = 10 ** 9 + 7

sys.setrecursionlimit(1000000)

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

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

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

ans = (
    'YES' if sum(i != q for i, q in enumerate(p, 1)) <= 2 else
    'NO'
)

print(ans)

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

q = p[:]
q.sort()

count = 0
for pp, qq in zip(p, q):
    if pp != qq:
        count += 1

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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())
a = [int(i) for i in readline().split()]
sa = sorted(a)

for i in range(n):
    for j in range(i+1,n):
        r = a[:]
        r[i], r[j] = r[j], r[i]
        
        if sa == r:
            print("YES")
            exit()

if a == sa:
    print("YES")
else:
    print("NO")

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

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, P):
    d = 0
    for a, b in zip(P, sorted(P)):
        if a != b:
            d += 1
    if d > 2:
        return 'NO'
    return 'YES'


def main():
    N = read_int()
    P = read_int_n()
    print(slv(N, P))


if __name__ == '__main__':
    main()

Solution (Python):
inf = 10 ** 18
P = 10 ** 9 + 7

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

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

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

Solution (Python):


def main():
    N = int( input())
    now = 0
    P = list( map( int, input().split()))
    for i in range(1, N+1):
        if i != P[i-1]:
            now += 1
    if now == 2 or now == 0:
        print("YES")
    else:
        print("NO")
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
A = list(range(1, N+1))
S = sum(1 if p != a else 0 for p, a in zip(P, A))
if S == 0 or S == 2:
    print('YES')
else:
    print('NO')

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

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

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7








N = int(sys.stdin.readline())
P = list(map(int, sys.stdin.readline().split()))
sp = list(sorted(P))
ok = False
for i, j in itertools.product(range(N), range(N)):
    P[i], P[j] = P[j], P[i]
    ok |= sp == P
    P[i], P[j] = P[j], P[i]
if ok:
    print('YES')
else:
    print('NO')

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

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

Solution (Python):
n, a = input(), map(int, raw_input().split())
print 'YES' if sum(x != y for x, y in zip(a, sorted(a))) <= 2 else 'NO'

Solution (Python):



def main():
    n = int(input())
    p = list(map(int, input().split()))
    mod_p = sorted(p)
    count = 0

    for pi, mod_pi in zip(p, mod_p):
        if pi != mod_pi:
            count += 1

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


if __name__ == '__main__':
    main()

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

num = 0
for i, p in enumerate(ps, 1):
    if i != p:
        num += 1
        if num > 2:
            print('NO')
            break
else:
    print('YES')

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

print('YES' if sum(i!=j for i,j in zip(range(1,n+1),A)) <= 2 else 'NO')

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

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

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

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

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

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

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

print("YES" if sum(x != i for i, x in enumerate(arr, 1)) <= 2 else "NO")

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    p = [int(s) for s in input().split()]
    p_sorted = sorted(p)
    rem = [(x, y) for x, y in zip(p, p_sorted) if x != y]
    if len(rem) <= 2:
        print('YES')
    else:
        print('NO')

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

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

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

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

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

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

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

Solution (Python):
N = input()
p = map(int, raw_input().split())
for i in range(N):
  for j in range(N):
    t = p[:]
    t[i],t[j] = t[j],t[i]
    if t==sorted(t):
      print "YES"
      exit(0)
print "NO"

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

Solution (Python):
LI=lambda:list(map(int,input().split()))
MI=lambda:map(int,input().split())
I=lambda:input()

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
cnt = 0
for i in range(n):
    if p[i] == q[i]:
        continue
    cnt+= 1

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

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

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

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

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

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

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

q = sorted(p)

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

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

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

Solution (Python):
N = int(input())
*P, = map(int,input().split())
Q = sorted(P)
c = sum(p!=q for p,q in zip(P,Q))
if c<=2:
    print("YES")
else:
    print("NO")

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

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

p_sort = sorted(p_arr)

ndiff = sum([1 for x,y in zip(p_arr, p_sort) if x!=y])

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

Solution (Python):
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""

def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a = iprow()
s = 0
for i in range(n):
    if a[i] != i+1:s+=1
if s==0 or s==2:j(1)
else:j(0)

Solution (Python):

import sys
INF = float("inf")


def yes():
    print("YES")  


def no():
    print("NO")  


def solve(N: int, p: "List[int]"):
    B = sorted(p)
    count = 0
    for bb, pp in zip(B, p):
        if bb != pp:
            count += 1
    if count <= 2:
        yes()
    else:
        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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
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 >= 3:
	print("NO")
else:
	print("YES")

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

ps = sorted(p)

c = len([1 for x,y in zip(p, ps) if x != y])
if c == 2 or c == 0:
    print("YES")
else:
    print("NO")

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

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

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n =getN()

nums = getList()
flag = 0
for i, x in enumerate(nums):
    if x != (i+1):
        flag += 1

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

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

same = 0
for i,p in enumerate(P):
    if i+1 == p:
        same += 1
print('YES' if same==N or same==N-2 else 'NO')

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return


def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return


def C():
    n = I()

    return


def D():
    n = I()

    return


def E():
    n = I()

    return


def F():
    n = I()

    return


if __name__ == "__main__":
    B()

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    arr = list(map(int, input().split()))
    kek = sorted(arr)
    
    cnt = 0
    for i in range(n):
        cnt += (arr[i] != kek[i])
    
    if cnt > 2:
        print("NO")
    else:
        print("YES")
    
    return 0

main()

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

cnt = 0
for i, j in enumerate(a):
    if i + 1 != j:
        cnt += 1

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
t = 0
for i in range(1,n+1):
    if p[i-1] != i:
        t += 1

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