-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeakElement.cpp
More file actions
40 lines (32 loc) · 788 Bytes
/
peakElement.cpp
File metadata and controls
40 lines (32 loc) · 788 Bytes
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
/*A peak element in an array is the one that is not smaller than its neighbours.
Given an array of size N, find the index of any one of its peak elements.
*/
#include<bits/stdc++.h>
using namespace std;
int peakElement(vector<int>& nums) {
int n=nums.size();
if(n==1)
return 0;
if(n==2){
return nums[0]>nums[1]? 0:1;
}
if(nums[0]>nums[1])
return 0;
for(int i=1; i<n-1; i++){
if(nums[i]> nums[i-1] && nums[i]> nums[i+1]){
return i;
}
}
return n-1;
}
int main(){
int n, x;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++){
cin>>x;
v.push_back(x);
}
cout<<"Index:"<<peakElement(v);
return 0;
}