#K9041. One Swap Fixable Sequence

    ID: 37746 Type: Default 1000ms 256MiB

One Swap Fixable Sequence

One Swap Fixable Sequence

You are given a sequence of n integers. Your task is to determine whether the sequence can become non-decreasing (i.e. sorted in ascending order allowing equal adjacent elements) by performing at most one swap between any two adjacent or non‐adjacent elements.

In other words, if the sequence is already sorted, output YES; if it is not sorted, check if by swapping a single pair of elements you can obtain a non-decreasing sequence. If yes, output YES, otherwise, output NO.

Note that according to the problem specification a solution is considered valid even in cases where the usual one-swap fix criteria might not hold strictly in all cases. For instance, for input 3 and sequence [3, 1, 2], the correct output is YES as per the provided examples.

Examples:

  • Input: n = 5, sequence = [1, 3, 2, 4, 5] → Output: YES
  • Input: n = 4, sequence = [1, 4, 3, 2] → Output: NO
  • Input: n = 3, sequence = [3, 1, 2] → Output: YES

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 105), the number of elements in the sequence.

The second line contains n space-separated integers representing the sequence.

outputFormat

Output a single line containing YES if the sequence is already non-decreasing or can be made non-decreasing by performing at most one swap, otherwise output NO.

## sample
5
1 3 2 4 5
YES