3105. Longest Strictly Increasing or Strictly Decreasing Subarray
Difficulty: Easy
Topics: Mid Level, Array, Weekly Contest 392
You are given an array of integers nums. Return the length of the longest subarray1 of nums which is either strictly increasing2 or strictly decreasing3.
Example 1:
- Input: nums = [1,4,3,3,2]
- Output: 2
-
Explanation:
- The strictly increasing subarrays of
numsare[1],[2],[3],[3],[4], and[1,4]. - The strictly decreasing subarrays of
numsare[1],[2],[3],[3],[4],[3,2], and[4,3]. - Hence, we return
2.
- The strictly increasing subarrays of
Example 2:
- Input: nums = [3,3,3,3]
- Output: 1
-
Explanation:
- The strictly increasing subarrays of
numsare[3],[3],[3], and[3]. - The strictly decreasing subarrays of
numsare[3],[3],[3], and[3]. - Hence, we return
1.
- The strictly increasing subarrays of
Example 3:
- Input: nums = [3,2,1]
- Output: 3
-
Explanation:
- The strictly increasing subarrays of
numsare[3],[2], and[1]. - The strictly decreasing subarrays of
numsare[3],[2],[1],[3,2],[2,1], and[3,2,1]. - Hence, we return
3.
- The strictly increasing subarrays of
Example 4:
- Input: nums = [1]
- Output: 1
Example 5:
- Input: nums = [1,2,3,4,5]
- Output: 5
Example 6:
- Input: nums = [5,4,3,2,1]
- Output: 5
Example 7:
- Input: nums = [1,3,2,4,3]
- Output: 2
Example 8:
- Input: nums = [2,2,1,2,3]
- Output: 3
Example 9:
- Input: nums = [1,2,1,2,3,4]
- Output: 4
Example 10:
- Input: nums = [10,9,8,7,1,2]
- Output: 4
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50
Solution:
We use a single left-to-right pass over nums, maintaining the current length of a strictly increasing run and a strictly decreasing run. Whenever a comparison breaks a run, the corresponding counter resets to 1. The answer is the maximum run length seen.
Approach
- Initialize
inc = 1anddec = 1because every single element is a valid monotonic subarray of length1. - Initialize
ans = 1for the same reason. - Iterate from index
1ton - 1:- If
nums[i] > nums[i - 1], extend the increasing run:inc++, and resetdec = 1. - If
nums[i] < nums[i - 1], extend the decreasing run:dec++, and resetinc = 1. - If
nums[i] == nums[i - 1], both strict runs are broken: resetinc = 1anddec = 1.
- If
- After each step, update
ans = max(ans, inc, dec). - Return
ans.
Let's implement this solution in PHP: 3105. Longest Strictly Increasing or Strictly Decreasing Subarray
<?php
/**
* @param Integer[] $nums
* @return Integer
*/
function longestMonotonicSubarray(array $nums): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
echo longestMonotonicSubarray([1, 4, 3, 3, 2]); // Output: 2
echo longestMonotonicSubarray([3, 3, 3, 3]); // Output: 1
echo longestMonotonicSubarray([3, 2, 1]); // Output: 3
echo longestMonotonicSubarray([1]); // Output: 1
echo longestMonotonicSubarray([1, 2, 3, 4, 5]); // Output: 5
echo longestMonotonicSubarray([5, 4, 3, 2, 1]); // Output: 5
echo longestMonotonicSubarray([1, 3, 2, 4, 3]); // Output: 2
echo longestMonotonicSubarray([2, 2, 1, 2, 3]); // Output: 3
echo longestMonotonicSubarray([1, 2, 1, 2, 3, 4]); // Output: 4
echo longestMonotonicSubarray([10, 9, 8, 7, 1, 2]); // Output: 4
?>
Explanation:
- A strictly increasing subarray can only continue when each next element is greater than the previous element.
- A strictly decreasing subarray can only continue when each next element is smaller than the previous element.
- Equal adjacent elements break both strict conditions, so both counters must reset.
- The variable
ansalways stores the longest valid run found so far. - The algorithm handles the entire array in one pass and does not need extra storage.
Complexity Analysis
-
Time Complexity:
O(n), wherenis the length ofnums. The array is scanned once. -
Space Complexity:
O(1), because only a constant number of counters are used.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Subarray
A subarray is a contiguous non-empty sequence of elements within an array. ↩ -
Strictly Increasing Array
An array is said to be strictly increasing if each element is strictly greater than its previous one (if exists). ↩ -
Strictly Decreasing Array
An array is said to be strictly decreasing if each element is strictly smaller than its previous one (if exists). ↩
Top comments (0)