Longest Strictly Increasing or Strictly Decreasing Subarray - Problem
You are given an array of integers nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.
A subarray is a contiguous non-empty sequence of elements within an array.
A strictly increasing subarray means each element is larger than the previous one.
A strictly decreasing subarray means each element is smaller than the previous one.
Input & Output
Example 1 — Mixed Pattern
$
Input:
nums = [1,4,3,3,2]
›
Output:
2
💡 Note:
The longest valid subarrays are [1,4] (strictly increasing) and [4,3] or [3,2] (strictly decreasing), all with length 2
Example 2 — Longer Increasing
$
Input:
nums = [3,3,3,3]
›
Output:
1
💡 Note:
All elements are equal, so no strictly increasing or decreasing subarray exists longer than 1
Example 3 — Perfect Sequence
$
Input:
nums = [3,1,5,6,9]
›
Output:
4
💡 Note:
The subarray [1,5,6,9] is strictly increasing with length 4
Constraints
- 1 ≤ nums.length ≤ 1000
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code