-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathpartitionPoint.cpp
More file actions
38 lines (30 loc) · 931 Bytes
/
partitionPoint.cpp
File metadata and controls
38 lines (30 loc) · 931 Bytes
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
// C++ program to find the element which is greater than
// left elements and smaller than right elements (or equal)
#include <bits/stdc++.h>
using namespace std;
int findElement(vector<int> &arr) {
// Iteratte through each elements
for(int i = 1; i < arr.size() - 1; i++) {
// to store maximum in left
int left = INT_MIN;
for(int j = 0; j < i; j++) {
left = max(left, arr[j]);
}
// to store minimum in right
int right = INT_MAX;
for(int j = i + 1; j < arr.size(); j++) {
right = min(right, arr[j]);
}
// check if current element is greater
// than left and smaller than right (or equal)
if(arr[i] >= left && arr[i] <= right) {
return arr[i];
}
}
return -1;
}
int main() {
vector<int> arr = {5, 1, 4, 3, 6, 8, 10, 7, 9};
cout << findElement(arr);
return 0;
}