#C10328. Taco Swap Sorting
Taco Swap Sorting
Taco Swap Sorting
You are given an array A of size n consisting of integers from 1 to n (not necessarily in order). You are allowed to perform a swap operation on any two distinct indices i and j if and only if |i - j| ≤ 2. Your task is to determine whether it is possible to sort the array in non-decreasing order using these operations.
Formal Description:
Given an integer n and an array A of n integers where A is a permutation of the numbers 1 through n, you may swap any two elements A[i] and A[j] if |i - j| ≤ 2 (indices are 1-indexed). Determine if it is possible to sort A in increasing order using a series of such swaps. If it is possible, output "YES", otherwise output "NO".
The constraint on the swap is equivalent to allowing a swap if the two positions are either adjacent or have exactly one element between them.
Note: All formulas are represented in LaTeX format when needed, for example the condition is \(|i-j|\le 2\).
inputFormat
The first line contains a single integer n — the size of the array A.
The second line contains n space-separated integers representing the array A.
outputFormat
Output a single line containing "YES" if it is possible to sort the array using the allowed swap operations, or "NO" if it is not possible.
## sample4
3 1 4 2
YES
</p>