#C3084. One Swap Sortability Check
One Swap Sortability Check
One Swap Sortability Check
You are given an array of integers. Your task is to determine whether the array can be transformed into a strictly increasing sequence by performing at most one swap between any two elements. An array A is strictly increasing if it satisfies the condition $$A_1 < A_2 < \cdots < A_n$$.
Example:
- For the array [1, 5, 3, 4, 2], swapping the elements 5 and 2 results in [1, 2, 3, 4, 5], which is strictly increasing; hence the answer is True.
- For the array [4, 3, 2, 1], no single swap can make the sequence strictly increasing; hence the answer is False.
You need to read the input from standard input (stdin) and output the result to standard output (stdout) as either True
or False
.
inputFormat
The input consists of two lines:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single line with either True
if the array can be sorted into a strictly increasing sequence by at most one swap, or False
otherwise.
5
1 2 3 4 5
True