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 = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      continue
    else:
      t += 1
    print(t)
  if t == 2 or t == 3 or t == 4:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      continue
    else:
      t += 1
    print(t)
  if t >= 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      t += 1
    else:
      continue
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      continue
    else:
      t += 1
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      continue
    else:
      t += 1
    print(t)
  if t == 3 or t == 4:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      continue
    else:
      t += 1
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
q = sorted(p)
if p == q:
  print("Yes")
else:
  t = 0
  for i in range(N):
    if p[i] != q[i]:
      t += 1
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N):
    for j in range(N):
      pi, pj = p[i], p[j]
      p[i], p[j] = pj, pi
      if p == sorted(p):
        t += 1
      else:
        continue
  if t == 0:
    print("No")
  else:
    print("Yes")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  q = sorted(p)
  t = 0
  for i in range(N):
    if p[i] == q[i]:
      t += 1
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
if p == sorted(p):
  print("Yes")
else:
  t = 0
  for i in range(N-1):
    if p[i+1] == p[i] + 1:
      t += 1
    else:
      continue
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
q = sorted(p)
if p == q:
  print("Yes")
else:
  t = 0
  for i in range(N):
    if p[i] != q[i]:
      t += 1
  if t == 2:
    print("Yes")
  else:
    print("No")

N = int(input())
p = input().split()
p = [int(num) for num in p]
q = sorted(p)
if p == q:
  print("YES")
else:
  t = 0
  for i in range(N):
    if p[i] != q[i]:
      t += 1
      #print(p[i],q[i],t)
    else:
      continue
  if t == 2:
    print("YES")
  else:
    print("NO")

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted