#include<bits/stdc++.h>
using namespace std;
int findPeakElement(vector<int>& nums) {
if(nums.size()==0){
return -1;
}
int l=0,r=nums.size()-1;
int ans;
while(l<=r){
if(l==r){
ans= l; // if single element
break;
}
int m=l+(r-l)/2;
if(m==0){
if(nums[m]>=nums[m+1]){ // if at left border
return m;
}
}
if(m==nums.size()-1){
if(nums[m]>=nums[m-1]){ // if at right border
return m;
}
}
if(nums[m]>nums[m+1] && nums[m]>nums[m-1]){ // if current mid is peak element
return m;
}
if(m>0 && nums[m-1]>nums[m]){
r=m-1; // if there is a greater element to left then there's definitely a peak to the left
}else if(m<nums.size()-1 && nums[m+1]>nums[m]){
l=m+1;
}
}
return ans;
}