#K43662. Almost Sorted Array
Almost Sorted Array
Almost Sorted Array
You are given an array of integers. An array is considered almost sorted if for every adjacent pair of elements \(a_i\) and \(a_{i+1}\), the absolute difference satisfies \(|a_i - a_{i+1}| \le 1\). Otherwise, the array is not almost sorted.
Your task is to determine whether each given array is almost sorted. For an array that meets the condition, output YES
, otherwise output NO
.
Example:
Input: 5 1 1 2 2 3</p>Output: YES
inputFormat
The first line of input contains an integer \(t\) representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(n\) representing the length of the array.
- The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing YES
if the array is almost sorted, or NO
otherwise.
3
5
1 1 2 2 3
4
1 3 5 5
5
5 5 5 5 5
YES
NO
YES
</p>