Description

An array is monotonic if it is either entirely non-increasing or entirely non-decreasing. Given an integer array nums, return true if the array is monotonic, false otherwise.

Examples

Input:nums = [1,2,2,3]
Output:true
Explanation:

Non-decreasing.

Input:nums = [6,5,4,4]
Output:true
Explanation:

The array is monotonically non-increasing: 6 >= 5 >= 4 >= 4.

Input:nums = [1,3,2]
Output:false
Explanation:

The array is neither non-increasing nor non-decreasing: 1 < 3 but 3 > 2.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!