{"id":9845,"date":"2022-09-26T10:09:13","date_gmt":"2022-09-26T17:09:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9845"},"modified":"2022-09-26T10:10:19","modified_gmt":"2022-09-26T17:10:19","slug":"leetcode-2419-longest-subarray-with-maximum-bitwise-and","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2419-longest-subarray-with-maximum-bitwise-and\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2419.\u00a0Longest Subarray With Maximum Bitwise AND"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;of size&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>Consider a&nbsp;<strong>non-empty<\/strong>&nbsp;subarray from&nbsp;<code>nums<\/code>&nbsp;that has the&nbsp;<strong>maximum<\/strong>&nbsp;possible&nbsp;<strong>bitwise AND<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In other words, let&nbsp;<code>k<\/code>&nbsp;be the maximum value of the bitwise AND of&nbsp;<strong>any<\/strong>&nbsp;subarray of&nbsp;<code>nums<\/code>. Then, only subarrays with a bitwise AND equal to&nbsp;<code>k<\/code>&nbsp;should be considered.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the length of the&nbsp;<strong>longest<\/strong>&nbsp;such subarray<\/em>.<\/p>\n\n\n\n<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.<\/p>\n\n\n\n<p>A&nbsp;<strong>subarray<\/strong>&nbsp;is a contiguous sequence of elements within an array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,3,2,2]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe maximum possible bitwise AND of a subarray is 3.\nThe longest subarray with that value is [3,3], so we return 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nThe maximum possible bitwise AND of a subarray is 4.\nThe longest subarray with that value is [4], so we return 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Find the largest number<\/strong><\/h2>\n\n\n\n<p>a &amp; b &lt;= a<br>a &amp; b &lt;= b<br>if b > a, a &amp; b &lt; b, we choose to start a new sequence of &#8220;b&#8221; instead of continuing with &#8220;ab&#8221;<\/p>\n\n\n\n<p>Basically, we find the largest number in the array and count the longest sequence of it. Note, there will be some tricky cases like.<br>b b b b a b<br>b a b b b b<br>We need to return 4 instead of 1.<\/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 longestSubarray(vector<int>& nums) {\n    int ans = 0;\n    int best = 0;\n    for (int i = 0, l = 0; i < nums.size(); ++i) {\n      if (nums[i] > best) {\n        best = nums[i]; \n        ans = l = 1;\n      } else if (nums[i] == best) {\n        ans = max(ans, ++l);\n      } else {\n        l = 0;\n      }\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;of size&nbsp;n. Consider a&nbsp;non-empty&nbsp;subarray from&nbsp;nums&nbsp;that has the&nbsp;maximum&nbsp;possible&nbsp;bitwise AND. In other words, let&nbsp;k&nbsp;be the maximum value of the bitwise AND of&nbsp;any&nbsp;subarray&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[786,20,16,88,177],"class_list":["post-9845","post","type-post","status-publish","format-standard","hentry","category-array","tag-and","tag-array","tag-bit","tag-greedy","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9845","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=9845"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9845\/revisions"}],"predecessor-version":[{"id":9848,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9845\/revisions\/9848"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}