Solution
- Time Complexity: O(n)
- Space Complexity: O(1)
func longestMonotonicSubarray(nums []int) int {
if len(nums) < 2 {
return len(nums)
}
res := 1
inc, dec := 1, 1
for i := 0; i < len(nums)-1; i++ {
if nums[i] < nums[i+1] {
// strictly increasing: increment the increasing counter and reset the decreasing counter
inc++
dec = 1
} else if nums[i] > nums[i+1] {
// strictly decreasing: increment the decreasing counter and reset the increasing counter
dec++
inc = 1
} else {
// when elements are equal, neither increasing nor decreasing
inc, dec = 1, 1
}
res = max(res, max(inc, dec))
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}Explanation
The problem asks us to find the length of the longest contiguous subarray that is either strictly increasing or strictly decreasing.
- A strictly increasing subarray is one where every element is greater than its previous element (e.g.,
[1, 2, 3]). - A strictly decreasing subarray is one where every element is less than its previous element (e.g.,
[3, 2, 1]).
The solution maintains two counters:
incfor the current strictly increasing subarray.decfor the current strictly decreasing subarray.
At each step, based on the relationship between consecutive elements, we update these counters and keep track of the maximum length encountered.
Approach
- Step 1: Check if the array has less than two elements. If yes, return its length since a single element (or empty array) is trivially monotonic.
- Step 2: Initialize two counters,
incanddec, to 1 and a variableresto keep track of the maximum subarray length found. - Step 3: Iterate through the array comparing each element with the next:
- If the current element is less than the next, it indicates a strictly increasing subarray. Increment
incand resetdecto 1. - If the current element is greater than the next, it indicates a strictly decreasing subarray. Increment
decand resetincto 1. - If the elements are equal, reset both counters to 1 as the monotonicity is broken.
- If the current element is less than the next, it indicates a strictly increasing subarray. Increment
- Step 4: Update
reswith the maximum value betweenincanddecafter each comparison. - Step 5: Return the value of
reswhich represents the longest monotonic subarray.
Example Walkthrough
Example 1:
Input: nums = [1, 3, 5, 4, 2]
- Start with
inc = 1,dec = 1,res = 1. - Iteration 1: Compare
1and3:1 < 3→inc = 2,dec = 1; updateres = 2. - Iteration 2: Compare
3and5:3 < 5→inc = 3,dec = 1; updateres = 3. - Iteration 3: Compare
5and4:5 > 4→dec = 2,inc = 1;resremains3. - Iteration 4: Compare
4and2:4 > 2→dec = 3,inc = 1; updateres = 3(the maximum remains 3).
The longest strictly monotonic subarray is of length 3.
Example 2:
Input: nums = [9, 7, 4, 2, 3, 5]
- Start with
inc = 1,dec = 1,res = 1. - Iteration 1: Compare
9and7:9 > 7→dec = 2,inc = 1; updateres = 2. - Iteration 2: Compare
7and4:7 > 4→dec = 3,inc = 1; updateres = 3. - Iteration 3: Compare
4and2:4 > 2→dec = 4,inc = 1; updateres = 4. - Iteration 4: Compare
2and3:2 < 3→inc = 2,dec = 1;resremains4. - Iteration 5: Compare
3and5:3 < 5→inc = 3,dec = 1;resremains4.
The longest strictly monotonic subarray is of length 4.
Complexity Analysis
-
Time Complexity:
The solution iterates over the array once, making it O(n) wherenis the number of elements in the array. -
Space Complexity:
The solution uses a constant amount of extra space (a few counters and a variable for the result), making it O(1).

