# Longest Strictly Monotonic Subarray
Given an integer array `nums`, return the length of its longest contiguous subarray that is either strictly increasing or strictly decreasing.
Strictly increasing means each element after the first is greater than its immediate predecessor. Strictly decreasing means each is smaller. Equal neighboring values do not satisfy either strict comparison. A one-element subarray qualifies.
### Examples
**Example 1**
```text
Input: nums = [2,5,8,6,4,1]
Output: 4
```
The subarray `[8,6,4,1]` is strictly decreasing and has length four. The initial increasing subarray `[2,5,8]` has length three. A subarray that includes both an increase and a decrease does not qualify, so no longer subarray works.
**Example 2**
```text
Input: nums = [1,2,2,3,4]
Output: 3
```
The last three elements `[2,3,4]` are strictly increasing. The equal neighboring twos prevent a longer increasing subarray from crossing them. There is no decreasing subarray longer than one.
### Constraints
- `1 <= nums.length <= 50`.
- `1 <= nums[i] <= 50`.
### Clarifying Questions
- Must the selected elements be adjacent? Yes; this is a subarray, not a subsequence.
- Can the direction change inside a qualifying subarray? No; all its adjacent comparisons must be increases, or all must be decreases.
- Can an equal pair be included in a qualifying subarray of length greater than one? No.
- Does a single element count? Yes; the array is nonempty, so the answer is at least one.
### C++ Interface
```cpp
class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
}
};
```
### Reasoning Checkpoints
- Enforce contiguity and one strict direction throughout the selected interval.
- Handle equal neighbors and the single-element boundary correctly.
- Prove the returned maximum length and justify resource use.
```hint Track the two suffix lengths
As you scan the array, keep the lengths of the strictly increasing and strictly decreasing subarrays ending at the current position. An unsuccessful strict comparison resets the corresponding suffix length to one.
```
Overview: Find the longest contiguous subarray that increases strictly or decreases strictly, with equal neighbors and single-element cases explained.
Given an integer array nums, return the length of its longest contiguous subarray that is either strictly increasing or strictly decreasing.
Strictly increasing means each element after the first is greater than its immediate predecessor. Strictly decreasing means each is smaller. Equal neighboring values do not satisfy either strict comparison. A one-element subarray qualifies.
Examples
Example 1
Input: nums = [2,5,8,6,4,1]
Output: 4
The subarray [8,6,4,1] is strictly decreasing and has length four. The initial increasing subarray [2,5,8] has length three. A subarray that includes both an increase and a decrease does not qualify, so no longer subarray works.
Example 2
Input: nums = [1,2,2,3,4]
Output: 3
The last three elements [2,3,4] are strictly increasing. The equal neighboring twos prevent a longer increasing subarray from crossing them. There is no decreasing subarray longer than one.
Constraints
1 <= nums.length <= 50
.
1 <= nums[i] <= 50
.
Clarifying Questions Guidance
Must the selected elements be adjacent? Yes; this is a subarray, not a subsequence.
Can the direction change inside a qualifying subarray? No; all its adjacent comparisons must be increases, or all must be decreases.
Can an equal pair be included in a qualifying subarray of length greater than one? No.
Does a single element count? Yes; the array is nonempty, so the answer is at least one.
C++ Interface
class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
}
};
Reasoning Checkpoints
Enforce contiguity and one strict direction throughout the selected interval.
Handle equal neighbors and the single-element boundary correctly.
Prove the returned maximum length and justify resource use.