Watch 10 video solutions for Longest Strictly Increasing or Strictly Decreasing Subarray, a easy level problem involving Array. This walkthrough by NeetCodeIO has 7,563 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: nums = [1,4,3,3,2]
Output: 2
Explanation:
The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].
The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].
Hence, we return 2.
Example 2:
Input: nums = [3,3,3,3]
Output: 1
Explanation:
The strictly increasing subarrays of nums are [3], [3], [3], and [3].
The strictly decreasing subarrays of nums are [3], [3], [3], and [3].
Hence, we return 1.
Example 3:
Input: nums = [3,2,1]
Output: 3
Explanation:
The strictly increasing subarrays of nums are [3], [2], and [1].
The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].
Hence, we return 3.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50Problem Overview: You are given an integer array and need the length of the longest contiguous subarray that is either strictly increasing or strictly decreasing. Adjacent elements must follow the rule a[i] > a[i-1] for increasing or a[i] < a[i-1] for decreasing. Equal values break the streak and start a new subarray.
Approach 1: Two Pointers Approach (O(n) time, O(1) space)
This method treats increasing and decreasing segments as separate windows. Use two pointers to expand a window while the order remains strictly increasing or strictly decreasing. When the condition breaks (for example, nums[i] <= nums[i-1] for increasing), reset the window start and continue scanning. Track the maximum length across both types of segments. The array is traversed once, making this approach efficient and easy to reason about when working with contiguous patterns in array problems.
Approach 2: Single Pass with State Variables (O(n) time, O(1) space)
This approach keeps two running counters: one for the current strictly increasing length and one for the strictly decreasing length. While iterating through the array, update these counters based on the comparison between nums[i] and nums[i-1]. If the current value is greater, increment the increasing counter and reset the decreasing counter; if smaller, do the opposite. If equal, reset both counters to 1. Track the global maximum during the scan. This technique is essentially a lightweight state machine and is common in linear scans over array problems that detect ordered segments.
Recommended for interviews: The single-pass state variable solution is typically what interviewers expect. It shows that you can detect patterns while scanning the array once and maintain minimal state. The two pointers method still demonstrates solid reasoning about contiguous windows and aligns well with problems that resemble two pointers or window-style traversal.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | When you prefer explicit window boundaries while scanning increasing or decreasing segments |
| Single Pass with State Variables | O(n) | O(1) | Best general solution for interviews and competitive programming with minimal logic |