#C5566. Can Sort Heroes
Can Sort Heroes
Can Sort Heroes
You are given n heroes with integer strengths. Your task is to determine if the heroes can be arranged in ascending order by strength using at most one adjacent swap. More formally, if the heroes are already sorted in non-decreasing order, the answer is YES
. Otherwise, if the list can be sorted by performing exactly one swap between a single pair of heroes (i.e. there is exactly one inversion in the list), then the answer is YES
. In all other cases, output NO
.
The inversion count is defined as the number of pairs \((i, j)\) with \( i a_j\). Use the condition that if the inversion count is exactly one then only one swap would bring the list to sorted order.
Note: Input is provided from stdin
and the output should be printed to stdout
.
inputFormat
The first line contains a single integer n representing the number of heroes. The second line contains n space-separated integers representing the strengths of the heroes.
outputFormat
Output a single line containing either YES
if the heroes can be sorted into ascending order with at most one swap, or NO
otherwise.## sample
4
1 3 2 4
YES