#C4513. Monotonic Array Verification
Monotonic Array Verification
Monotonic Array Verification
Given an array of n integers, determine whether the array is monotonic. An array is considered monotonic if it is either entirely non-decreasing or entirely non-increasing.
More formally, an array A is monotonic if it satisfies one of the following conditions for all valid indices i:
- Non-decreasing: $$A_i \leq A_{i+1}$$
- Non-increasing: $$A_i \geq A_{i+1}$$
Note that an empty array or an array with a single element is considered monotonic.
inputFormat
The input is given in two lines:
- The first line contains a single integer
n
which represents the number of elements in the array. (n
can be 0, representing an empty array.) - The second line contains
n
space-separated integers representing the array elements. Ifn
is 0, the second line may be empty.
outputFormat
Output a single line with either True
if the array is monotonic, or False
otherwise.
5
1 2 2 3 4
True