{"id":5959,"date":"2019-12-14T12:36:35","date_gmt":"2019-12-14T20:36:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5959"},"modified":"2019-12-14T12:51:47","modified_gmt":"2019-12-14T20:51:47","slug":"leetcode-1287-element-appearing-more-than-25-in-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1287-element-appearing-more-than-25-in-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1287. Element Appearing More Than 25% In Sorted Array"},"content":{"rendered":"\n<p>Given an&nbsp;integer array&nbsp;<strong>sorted<\/strong>&nbsp;in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.<\/p>\n\n\n\n<p>Return that integer.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> arr = [1,2,2,6,6,6,6,7,10]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^4<\/code><\/li><li><code>0 &lt;= arr[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Linear Scan<\/strong><\/h2>\n\n\n\n<p>if arr[i] == arr[i + len\/4] => arr[i] is the special integer.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findSpecialInteger(vector<int>& arr) {\n    int s = arr.size() \/ 4;\n    for (int i = 0; i + s < arr.size(); ++i)\n      if (arr[i] == arr[i + s]) return arr[i];\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Binary Search<\/strong><\/h2>\n\n\n\n<p>The answer must be one of (s[0], s[l\/4], s[l\/2], s[l*3\/4])<br>Using binary search to find the range of each number, the one has more than 1\/4 of total elements is the answer.<\/p>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findSpecialInteger(vector<int>& arr) {\n    const int s = arr.size() \/ 4;    \n    for (int i = 0; i < 4; ++i) {\n      const int x = arr[i * s];\n      const auto [it1, it2] = equal_range(begin(arr), end(arr), x);\n      if (it2 - it1 > s) return x;      \n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;integer array&nbsp;sorted&nbsp;in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,222],"class_list":["post-5959","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5959"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5959\/revisions"}],"predecessor-version":[{"id":5962,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5959\/revisions\/5962"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}