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)


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