-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_852.cpp
43 lines (39 loc) · 1.23 KB
/
Leetcode_852.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
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
// Function to find the peak index in a mountain array
int peakIndexInMountainArray(vector<int>& arr) {
int st = 0, end = arr.size()-1, mid;
// Binary search to find the peak element
while(st <= end) {
mid = st + (end - st)/2;
// If mid is the first element, return the next element
if(mid == 0){
return (mid+1);
}
// Check if mid is the peak element
else if(arr[mid-1] <= arr[mid] && arr[mid] >= arr[mid+1]) {
return mid;
}
// If the element at mid is greater than the previous element, move to the right half
if(arr[mid] > arr[mid -1]) {
st = mid+1;
}
// Otherwise, move to the left half
else {
end = mid-1;
}
}
// Return -1 if no peak is found (should not happen in a valid mountain array)
return -1;
}
};
int main() {
Solution s;
vector<int> arr = {0,1,0};
// Print the peak index of the mountain array
cout<<s.peakIndexInMountainArray(arr);
return 0;
}