-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeak element.cpp
73 lines (66 loc) · 1.52 KB
/
Peak element.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int peakElement(vector<int> &arr) {
// Your code here
int n = arr.size();
int low = 0,high = n-1;
while(low<high)
{
int mid = (low+high)/2;
if(arr[mid]>arr[mid+1])
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> a;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
a.push_back(number);
}
Solution ob;
int idx = ob.peakElement(a);
int n = a.size();
bool f = 0;
if (idx < 0 and idx >= n)
cout << "false" << endl;
else {
if (n == 1 and idx == 0)
f = 1;
else if (idx == 0 and a[0] > a[1])
f = 1;
else if (idx == n - 1 and a[n - 1] > a[n - 2])
f = 1;
else if (a[idx] > a[idx + 1] and a[idx] > a[idx - 1])
f = 1;
else
f = 0;
if (f)
cout << "true" << endl;
else
cout << "false" << endl;
}
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends