Skip to content

Commit ea24c83

Browse files
Create 162. Find Peak Element.cpp
1 parent 3dbfa63 commit ea24c83

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

162. Find Peak Element.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
TC = O (nlogn)
2+
class Solution {
3+
public:
4+
int findPeakElement(vector<int>& nums) {
5+
int low = 0 , high = nums.size()-1;
6+
while(low < high) {
7+
int mid = (low + high)/2;
8+
if(nums[mid] > nums[mid+1]) high = mid;
9+
else low = mid + 1 ;
10+
}
11+
return low;
12+
}
13+
};
14+
15+
TC = O (n)
16+
class Solution {
17+
public:
18+
int findPeakElement(vector<int>& nums) {
19+
return max_element(nums.begin() , nums.end()) - nums.begin() ;
20+
}
21+
};
22+
23+
TC = O(n)
24+
class Solution {
25+
public:
26+
int findPeakElement(vector<int>& nums) {
27+
int n = nums.size() , ind = 0;
28+
for(int i = 0; i < n-1 ; i++){
29+
if(nums[i] > nums[i+1] ) return i;
30+
}
31+
return n-1;
32+
}
33+
};
34+

0 commit comments

Comments
 (0)