Practice/Oracle/Leetcode 845. Longest Mountain in Array
CodingMust
You are given an integer array. Your task is to identify the longest contiguous subarray that forms a "mountain" pattern and return its length.
A mountain subarray is defined as a sequence where:
If no valid mountain exists in the array, return 0.
Example 1:
Input: arr = [2, 1, 4, 7, 3, 2, 5] Output: 5 Explanation: The subarray [1, 4, 7, 3, 2] forms a valid mountain. It increases from 1→4→7, then decreases 7→3→2.
Example 2:
Input: arr = [2, 2, 2] Output: 0 Explanation: All elements are equal, so no strictly increasing or decreasing sequence exists.
Example 3:
Input: arr = [0, 1, 2, 3, 4, 5] Output: 0 Explanation: The array only increases. A valid mountain requires both ascending and descending portions.
Example 4:
Input: arr = [1, 3, 2] Output: 3 Explanation: The entire array forms a mountain: 1→3 (up), then 3→2 (down).