LeetCode 845, Medium. Topics: Array, Two Pointers, Dynamic Programming, Enumeration. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 21 parametrized pytest cases, and a playground notebook:
Problem
You may recall that an array arr is a mountain array if and only if:
arr.length >= 3
- There exists some index
i (0-indexed) with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given an integer array arr, return the length of the longest subarray, which is a mountain. Return 0 if there is no mountain subarray.
Examples
Constraints
- 1 <= arr.length <= 10^4
- 0 <= arr[i] <= 10^4
Follow up:
- Can you solve it using only one pass?
- Can you solve it in
O(1) space?
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026