You may recall that an array arr is a mountain array if and only if:


	arr.length &gt;= 3
	There exists some index i (0-indexed) with 0 &lt; i &lt; arr.length - 1 such that:
	
		arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]
		arr[i] &gt; arr[i + 1] &gt; ... &gt; 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.

&nbsp;
Example 1:

Input: arr = [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.


Example 2:

Input: arr = [2,2,2]
Output: 0
Explanation: There is no mountain.


&nbsp;
Constraints:


	1 &lt;= arr.length &lt;= 104
	0 &lt;= arr[i] &lt;= 104


&nbsp;
Follow up:


	Can you solve it using only one pass?
	Can you solve it in O(1) space?


class Solution:
    def longestMountain(self, arr: List[int]) -> int:
        if len(arr) < 3:
            return 0
        
        max_up, max_down = [1 for _ in arr], [1 for _ in arr]
        
        for i, x in enumerate(arr):
            if i == 0:
                continue
            if x > arr[i - 1]:
                max_up[i] = max_up[i - 1] + 1
            
        for j, y in reversed(list(enumerate(arr))):
            if j == len(arr) - 1:
                continue
            if y > arr[j + 1]:
                max_down[j] = max_down[j + 1] + 1
                
        return max(
            x + y - 1 if x > 1 and y > 1 else 0 
            for x, y in zip(max_up, max_down)
        )
class Solution {
    public int longestMountain(int[] arr) {
        if (arr.length < 3)
            return 0;
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (isPeak(arr, i)) {
                int leftLength = left(arr, i);
                int rightLength = right(arr, i);
                max = Math.max(max, leftLength + rightLength + 1);
            }
        }
        return max;
    }

    public int left(int[] arr, int i) {
        int j = i - 1;
        while (j >= 0 && arr[j] < arr[j + 1])
            j--;
        return i - (j + 1);
    }

    public int right(int[] arr, int i) {
        int j = i + 1;
        while (j < arr.length && arr[j - 1] > arr[j])
            j++;
        return j - (i + 1);
    }

    public boolean isPeak(int[] arr, int i) {
        return i - 1 >= 0 && i + 1 < arr.length && arr[i - 1] < arr[i] && arr[i + 1] < arr[i];
    }
}
class Solution {
public:
    int longestMountain(vector<int>& arr) {
     vector<int> v;
        for(int i=0;i<arr.size()-1;i++)
        {
            if(arr[i+1]>arr[i])v.push_back(1);
            else if(arr[i+1]<arr[i])v.push_back(-1);
            else 
                v.push_back(0);
        }
        int i=0;
        int ans=0;
        while(i<v.size())
        {
            int c1=0,c2=0;
            while(i<v.size() and v[i]==1)
            {
                c1++;
                i++;
            }
            while(i<v.size() and v[i]==-1)
            {
                c2++;
                i++;
            }
            while(i<v.size() and v[i]==0)
            {
                i++;
            }
            if(c1!=0 and c2!=0)
            {
                ans=max(ans,c1+c2+1);
            }
        }
        return ans;
    }
};
var longestMountain = function(arr) {
    let len=arr.length;
    let climbUp=false;
    let climbDown=false;
    let countUp=0;
    let countDown=0;
    let distance=0;
    let max=0; 
    if(len<3){
        return 0;
    }
    for(i=0;i<len;i++){
        if(arr[i-1]<arr[i]){
        countUp++;
        climbUp=true;   
        }else if(arr[i-1]>arr[i] && climbUp){
        countDown++; 
        climbDown=true;
        }
        if((arr[i-1]>=arr[i] && arr[i]<=arr[i+1]) || (i==len-1 && climbDown==true)){
            distance=countUp+countDown+1;
            if(max<distance && climbUp && climbDown){
                max=distance;
            }
            countUp=0;
            countDown=0;
            climbUp=false;
            climbDown=false;
        }
        if(arr[i+1]==arr[i]){
            climbUp=false;
            climbDown=false;
        }
    }
   return(max<3?0:max)
};
Longest Mountain in Array