Longest Strictly Increasing or Strictly Decreasing Subarray (LeetCode 3105): Solution and Explanation

Easy
Array

Given an array of integers, return the length of the longest contiguous subarray that is strictly increasing or strictly decreasing. Equal neighbours do not count as either.

Intuition

Checking every subarray works for 50 elements, but it hides a simpler structure. Any strictly monotonic subarray ending at index i is a suffix of either the longest increasing run ending at i or the longest decreasing run ending at i.

Those two runs can be updated from index i-1 by looking at a single pair. If nums[i] > nums[i-1], the climb continues and the fall is over. If nums[i] < nums[i-1], the fall continues and the climb is over. If they are equal, neither strict run survives.

So keep two counters, inc and dec, that both describe runs ending at the current index. After each update the answer so far is the largest of best, inc and dec.

Approach: Two running lengths

  1. Set best = inc = dec = 1, since one element is a valid run of both kinds.
  2. For each i from 1, compare nums[i] with nums[i-1].
  3. On a rise, inc += 1 and dec = 1. On a drop, dec += 1 and inc = 1. On a tie, set both to 1.
  4. Update best = max(best, inc, dec) and return best after the loop.

Step-by-step example

nums = [1,4,3,3,2]
  1. Start with best = inc = dec = 1.
  2. 1 < 4: inc = 2, dec = 1, best = 2.
  3. 4 > 3: dec = 2, inc = 1, best stays 2.
  4. 3 = 3: both runs reset to 1.
  5. 3 > 2: dec = 2, inc = 1, best stays 2. Return 2.

Code: Python, Java, C++

1class Solution:
2    def longestMonotonicSubarray(self, nums: List[int]) -> int:
3        best = inc = dec = 1
4        for i in range(1, len(nums)):
5            if nums[i] > nums[i - 1]:
6                inc, dec = inc + 1, 1
7            elif nums[i] < nums[i - 1]:
8                inc, dec = 1, dec + 1
9            else:
10                inc = dec = 1
11            best = max(best, inc, dec)
12        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Two running lengths O(n)
Two separate passes O(n)
Brute force over all subarrays O(n²)
Time

O(n)

Each element is compared with its left neighbour once.

Space

O(1)

Only three counters are kept.

Other approaches

Brute force over all subarrays (time O(n²), space O(1))

For every start index, extend to the right while the subarray stays strictly increasing, then do the same for strictly decreasing, and keep the longest length.

Two separate passes (time O(n), space O(1))

Find the longest strictly increasing run in one pass and the longest strictly decreasing run in another, then return the larger. Same result with twice the scanning.

Common mistakes

  • Treating equal neighbours as part of a run. The problem asks for strict order, so a tie must reset both counters.
  • Resetting a counter to 0 instead of 1. The current element always forms a run of length 1 on its own.
  • Forgetting the single-element case, where the answer is 1 and the loop body never runs.
  • Only updating best when a run breaks, which misses a run that lasts until the end of the array.

Frequently asked questions

Why can both counters be tracked together?

Each comparison of a neighbour pair is a rise, a drop or a tie. A rise can only extend the increasing run, a drop only the decreasing run, and a tie neither, so one pass updates both in O(1).

What is the answer when every element is equal?

It is 1. Every pair is a tie, so both runs keep resetting to a single element.

Is this the same as the longest increasing subsequence?

No. A subarray must be contiguous, so a single break ends the run. The subsequence problem allows skipping elements and needs dynamic programming or binary search.