Monotonic Array Check
Given an array of integers, determine if the array is monotonic. An array is considered monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
[ [ 1, 2, 2, 3 ] ]
Explanation. The array [1, 2, 2, 3] is monotone increasing.
[ [ 6, 5, 4, 4 ] ]
Explanation. The array [6, 5, 4, 4] is monotone decreasing.
[ [ 1, 3, 2 ] ]
Explanation. The array [1, 3, 2] is neither monotone increasing nor decreasing.
[ [ 10, 10, 10, 10 ] ]
Explanation. The array [10, 10, 10, 10] is both monotone increasing and decreasing as all elements are the same.
Follow-up: How would you optimize your solution if multiple queries for different subarrays of the same array are expected?
The array will contain at least 1 and not more than 10,000 integers. Each integer will be between -10,000 and 10,000, inclusive.
- Views
- 3