classSolution { intsolution(vector<int>& arr, int l, int r){ if(l + 1 >= r) return0; int m = l + (r-l)/2; if(arr[m-1] >= arr[m] or arr[m+1] >= arr[m]) //if m is vally or horizon returnmax(solution(arr, l, m), solution(arr,m,r));
bool upperLeft = arr[m] < arr[m-1]; //top might be in left side
while(l < m and m < r) { //move m to top of mountain if(upperLeft) { if(arr[m-1] > arr[m]) m--; elseif(arr[m-1] == arr[m]) { returnmax(solution(arr,l,m-1), solution(arr,l + (r-l)/2, r)); } elsebreak; } else { if(arr[m+1] > arr[m]) m++; elseif(arr[m+1] == arr[m]) { returnmax(solution(arr,l,l + (r-l)/2), solution(arr,m+1,r)); } elsebreak; } }
if(m == l) returnsolution(arr, l + (r-l)/2, r); //if top is end of left just check right only if(m == r) returnsolution(arr,l, l + (r-l)/2); //if top is end of right just check left only
if(!(arr[m-1] < arr[m] and arr[m+1] < arr[m])) return0; //if m is not top return 0
int left = m, right = m; while(left > 0) { //find left end and right end if(arr[left] > arr[left-1]) left--; elsebreak; } while(right < arr.size()-1) { if(arr[right] > arr[right+1]) right++; elsebreak; }