File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 10
10
| 5| [ Arranging Coins(Easy)] ( https://leetcode.com/problems/arranging-coins/ ) | [ Solution] ( arranging_coin.cpp ) |
11
11
| 6| [ Find Smallest Letter Greater Than Target] ( https://leetcode.com/problems/find-smallest-letter-greater-than-target/ ) | [ Solution] ( smallest_letter_greater_than_target.cpp ) |
12
12
| 7| [ Kth Missing Positive Number] ( https://leetcode.com/problems/kth-missing-positive-number/ ) | [ Solution] ( kth_missing_positive_no.cpp ) |
13
- | 8| [ Search Insert Position] ( https://leetcode.com/problems/search-insert-position/ ) | Pending |
13
+ | 8| [ Search Insert Position] ( https://leetcode.com/problems/search-insert-position/ ) | [ Solution ] ( search_insert_position.cpp ) |
14
14
| 9| [ Peak Index in a Mountain Array] ( https://leetcode.com/problems/peak-index-in-a-mountain-array/ ) | Pending|
15
15
| 10| [ Count Negative Numbers in a Sorted Matrix] ( https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/ ) | Pending|
16
16
| 11| [ Intersection of Two Arrays] ( https://leetcode.com/problems/intersection-of-two-arrays/ ) | Pending|
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ int peakIndexInMountainArray (vector<int > &arr)
5
+ {
6
+ // linear search
7
+ // for(int i=0; i<arr.size()-1; i++){
8
+ // if(arr[i]>arr[i+1]) return i;
9
+ // }
10
+ // return arr.size()-1;
11
+
12
+ // binary search
13
+ int l = 0 , r = arr.size () - 1 , mid;
14
+ while (l < r)
15
+ {
16
+ mid = (l + r) / 2 ;
17
+ if (arr[mid] < arr[mid + 1 ])
18
+ l = mid + 1 ;
19
+ else
20
+ r = mid;
21
+ }
22
+ return l;
23
+ }
24
+
25
+ int main ()
26
+ {
27
+ int n;
28
+ cin >> n;
29
+ vector<int > v;
30
+ for (int i = 0 ; i < n; i++)
31
+ {
32
+ int temp;
33
+ cin >> temp;
34
+ v.push_back (temp);
35
+ }
36
+ cout<<peakIndexInMountainArray (v)<<endl;
37
+ return 0 ;
38
+ }
You can’t perform that action at this time.
0 commit comments