Longest Strictly Increasing or Strictly Decreasing Subarray

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. A subarray is a contiguous part of the array.

30:00

Longest Strictly Increasing or Strictly Decreasing Subarray
easy
Free written solution
Topics
Companies

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. A subarray is a contiguous part of the array.

Example 1:
Input: {"nums":[1,4,3,3,2]}
Output: 2
Constraints:
  • 1≤nums.length≤501 \leq \text{nums.length} \leq 50

  • 1≤nums[i]≤501 \leq \text{nums}[i] \leq 50

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,4,3,3,2]}

Find the longest stretch of 5 numbers that only climbs or only falls, with no equal neighbours.

inc -
dec -
best -

nums as heights · arrows compare each neighbour pair

14332

Both runs end at the current index. A rise extends the climbing run and resets the falling one, a drop does the opposite, and an equal pair resets both.

Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/8