Find the length of the longest contiguous subarray that is either strictly increasing or strictly decreasing.
Given an integer array nums, return the length of the longest contiguous subarray that is either:
A subarray must consist of consecutive elements from the original array.
Your task is to identify the maximum length among all such monotonic subarrays.
nums.nums[i] is an integer value at index i.1 <= nums.length1.Example 1
Input
nums = [1,2,3,2,1,0,4]
Output
4
Explanation
The longest strictly decreasing subarray is [3,2,1,0], which has length 4.
Example 2
Input
nums = [5,5,5]
Output
1
Explanation
No adjacent pair is strictly increasing or strictly decreasing, so the best subarray has length 1.
Example 3
Input
nums = [1,3,5,4,2]
Output
3
Explanation
The longest strictly increasing subarray is [1,3,5] and the longest strictly decreasing subarray is [5,4,2]. Both have length 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.