File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 30
30
| 3| [ Search in Rotated Sorted Array] ( https://leetcode.com/problems/search-in-rotated-sorted-array/ ) | Pending|
31
31
| 4| [ Search in Rotated Sorted Array II] ( https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ ) | Pending|
32
32
| 5| [ Find Minimum in Rotated Sorted Array] ( https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ ) | Pending|
33
- | 6| [ Find Peak Element] ( https://leetcode.com/problems/find-peak-element/ ) | Pending |
33
+ | 6| [ Find Peak Element] ( https://leetcode.com/problems/find-peak-element/ ) | [ Solution ] ( peak_ele.cpp ) |
34
34
| 7| [ Find Right Interval] ( https://leetcode.com/problems/find-right-interval/ ) | Pending|
35
35
| 8| [ Reach a Number] ( https://leetcode.com/problems/reach-a-number/ ) | Pending|
36
36
| 9| [ Maximum Value at a Given Index in a Bounded Array] ( https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/ ) | Pending|
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ int findPeakElement (vector<int > &num)
5
+ {
6
+ int low = 0 , high = num.size () - 1 ;
7
+ while (low < high - 1 )
8
+ {
9
+ int mid = (low + high) / 2 ;
10
+ if (num[mid] > num[mid - 1 ] && num[mid] > num[mid + 1 ])
11
+ return mid;
12
+ else if (num[mid] > num[mid + 1 ])
13
+ high = mid - 1 ;
14
+ else
15
+ low = mid + 1 ;
16
+ }
17
+ return num[low] > num[high] ? low : high;
18
+ }
19
+
20
+ int main ()
21
+ {
22
+ int n, target;
23
+ cin >> n;
24
+ vector<int > v;
25
+ for (int i = 0 ; i < n; i++)
26
+ {
27
+ int temp;
28
+ cin >> temp;
29
+ v.push_back (temp);
30
+ }
31
+ cout << findPeakElement (v) << endl;
32
+ return 0 ;
33
+ }
You can’t perform that action at this time.
0 commit comments