98. Longest Mountain in Array

MediumArrayArray

Given an integer array arr, return the length of the longest subarray that forms a mountain. A mountain subarray has strictly increasing then strictly decreasing elements, with at least 3 elements. If no mountain exists, return 0.

Input: Integer array arr.

Output: Integer — length of longest mountain subarray, or 0.

Examples

Example 1
Input: [2,1,4,7,3,2,5]
Output: 5
Explanation: [1,4,7,3,2] has length 5 and is a mountain.
Example 2
Input: [2,2,2]
Output: 0
Explanation: No mountain — no strictly increasing then decreasing.
Example 3
Input: [0,1,0]
Output: 3
Explanation: [0,1,0] is the whole array, a mountain of length 3.

Constraints

Asked by

IBMOracleGoogleMicrosoftAmazonMeta
Solve this problem in the editor →