Problem: 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.

Submission attempts (in order):
n, *a = map(int, input().split())
cnt = 0
for i in range(n):
    if sorted(a)[i] != a[i]:
        cnt +=1


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

n, *a = map(int, input().split())
li = []
sorted_a = sorted(a)
for i in range(n):
    if sorted_a[i] != a[i]:
        cnt +=1
        li.append(i)
        


def hantei(n, a, li):
    if len(li) >= 3:
        return('NO')

    elif len(li) <= 1:
        return('YES')

    else:
        a[li[0]], a[li[1]] = a[li[1]], a[li[0]]
        for i in range(n):
            if sorted_a[i] != a[i]:
                return('NO')
        return('YES')

    
print(hantei(n, a, li))

from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() 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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

n, *a = map(int, input().split())
cnt = 0
li = []


def hantei(s):
    for i in range(n-1):
        if s[i] > s[i+1]:
            return False
    return True
    

for i in range(n-1):
    if a[i] > a[i+1]:
        li.append(i+1)


if len(li) < 2:
    print('YES')

elif len(li) >= 2:
    a[li[0]], a[li[1]] = a[li[1]], a[li[0]]
    if hantei(a):
        print('YES')
    else:
        print('NO')

n, *a = map(int, input().split())
li = []
sorted_a = sorted(a)
for i in range(n):
    if sorted_a[i] != a[i]:
        cnt +=1
        li.append(i)
        


def hantei(n, a, li):
    if len(li) >= 3:
        return('NO')

    elif len(li) <= 1:
        return('YES')

    else:
        a[li[0]], a[li[1]] = a[li[1]], a[li[0]]
        print(li)
        for i in range(n):
            if sorted_a[i] != a[i]:
                return('NO')
        return('YES')

    
print(hantei(n, a, li))

from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() 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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp = num.copy()
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted