#K74287. Almost Sorted Array
Almost Sorted Array
Almost Sorted Array
You are given an array of integers. An array is considered an Almost Sorted Array if it is already sorted in non-decreasing order, or if by removing exactly one element the remainder of the array becomes sorted in non-decreasing order.
Formally, an array A = [a1, a2, ..., an] is sorted in non-decreasing order if for every index i (1 ≤ i < n), it holds that $$a_i \leq a_{i+1}$$. You may remove exactly one element (or none) to achieve this property.
Your task is to determine if the given array is Almost Sorted. Output YES
if it is, and NO
otherwise.
inputFormat
The input consists of two lines:
- The first line contains a single integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers representing the elements of the array.
outputFormat
Output a single line containing YES
if the array is Almost Sorted, otherwise output NO
.
3
10 5 7
YES