forked from VinayBelwal/Hactober-22-Programs-and-Projects-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LongestMountain.cpp
42 lines (40 loc) · 991 Bytes
/
LongestMountain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
int longestMountain(vector<int>& a) {
int largest=0;
int cnt=0;
int n=a.size();
for(int i=1;i<n-1;){
if(a[i]>a[i-1]&&a[i]>a[i+1]){
int j=i-1;
cnt=1;
while(j>=0&&a[j]<a[j+1]){
cnt++;
j--;
}
i=i+1;
while(i<n&&a[i]<a[i-1]){
cnt++;
i++;
}
largest=max(largest,cnt);
}
else
i++;
}
return largest;
}
int main(){
int n;
cout<<"Enter the size of array: ";
cin>>n;
vector<int> v;
cout<<"Enter the elements of the array ";
int x;
for(int i=0;i<n;i++){
cin>>x;
v.push_back(x);
}
int ans=longestMountain(v);
cout<<"The longest mountain length is "<<ans;
}