<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>and Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/and/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/and/</link>
	<description></description>
	<lastBuildDate>Mon, 26 Sep 2022 17:10:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>and Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/and/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 26 Sep 2022 17:09:13 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9845</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;of size&#160;n. Consider a&#160;non-empty&#160;subarray from&#160;nums&#160;that has the&#160;maximum&#160;possible&#160;bitwise AND. In other words, let&#160;k&#160;be the maximum value of the bitwise AND of&#160;any&#160;subarray&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/">花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array&nbsp;<code>nums</code>&nbsp;of size&nbsp;<code>n</code>.</p>



<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>



<ul><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>



<p>Return&nbsp;<em>the length of the&nbsp;<strong>longest</strong>&nbsp;such subarray</em>.</p>



<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>



<p>A&nbsp;<strong>subarray</strong>&nbsp;is a contiguous sequence of elements within an array.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,3,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Find the largest number</strong></h2>



<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>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int longestSubarray(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    int best = 0;
    for (int i = 0, l = 0; i &lt; nums.size(); ++i) {
      if (nums[i] &gt; best) {
        best = nums[i]; 
        ans = l = 1;
      } else if (nums[i] == best) {
        ans = max(ans, ++l);
      } else {
        l = 0;
      }
    }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/">花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
