All problems
Longest Mountain Subarray
Problem
Return the length of the longest contiguous mountain subarray: strictly increasing to a peak and then strictly decreasing, with at least one element on each side.
For every peak, expand left while increasing and right while decreasing, then continue scanning after the right end. Return 0 when no mountain exists.
Hint
A peak needs a strictly smaller neighbour on both sides before it can start a mountain.
Test cases
case 1
in: [[2,1,4,7,3,2,5]]
expected: 5
case 2
in: [[2,2,2]]
expected: 0
case 3
in: [[1,2,3,4,5]]
expected: 0
case 4
in: [[1,3,2,1,4,5,3]]
expected: 4
case 5
in: [[1,2]]
expected: 0
Starter codePython
def longest_mountain_subarray(arr):
# Your code here
pass5 test cases · run your code against them in the editor