forked from NiharRanjan53/HacktoberFest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mountain.cpp
48 lines (39 loc) · 991 Bytes
/
Mountain.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
43
44
45
46
47
48
#include <iostream>
#include <vector>
using namespace std;
int main() {
int arr[] = {5,6,1,2,3,4,5,4,3,2,0,1,2,3,-2,4};
int n = sizeof(arr)/sizeof(arr[0]);
int highest_length = 0;
for(int i=1;i<n-1;)
{
// trace peak element
if(arr[i]>arr[i-1] and arr[i]>arr[i+1])
{
int count=1;
int backward = i;
// for backward
while(backward>1 and arr[backward]>arr[backward-1])
{
count++;
backward--;
}
// for forward
while(i<(n-2) and arr[i]>arr[i+1])
{
i++;
count++;
}
if(count>highest_length)
{
highest_length=count;
}
}
else
{
i++;
}
}
cout<<highest_length;
return 0;
}