|
| 1 | +<h2><a href="https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and">2419. Longest Subarray With Maximum Bitwise AND</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code> of size <code>n</code>.</p> |
| 2 | + |
| 3 | +<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p> |
| 10 | + |
| 11 | +<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p> |
| 12 | + |
| 13 | +<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> nums = [1,2,3,3,2,2] |
| 20 | +<strong>Output:</strong> 2 |
| 21 | +<strong>Explanation:</strong> |
| 22 | +The maximum possible bitwise AND of a subarray is 3. |
| 23 | +The longest subarray with that value is [3,3], so we return 2. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> nums = [1,2,3,4] |
| 30 | +<strong>Output:</strong> 1 |
| 31 | +<strong>Explanation:</strong> |
| 32 | +The maximum possible bitwise AND of a subarray is 4. |
| 33 | +The longest subarray with that value is [4], so we return 1. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 41 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 42 | +</ul> |
0 commit comments