leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1287.cpp (476B)


0 class Solution {
1 public:
2 int findSpecialInteger(vector<int> &arr) {
3 const int n = size(arr), quarter = n / 4;
4 for (const int candidate : {arr[n / 4], arr[n / 2], arr[3 * n / 4]}) {
5 const int left = lower_bound(begin(arr), end(arr), candidate) - begin(arr);
6 const int right = upper_bound(begin(arr), end(arr), candidate) - begin(arr);
7 if (right - left > quarter) return candidate;
8 }
9 return -1;
10 }
11 };