<p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>

<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>

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

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

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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<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 class="example">Example 2:</strong></p>

<pre>
<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>&nbsp;</p>
<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>

class LongestSubarray:
    def __init__(self, n: int, start: int, end: Optional[int] = None):
        self.n = n
        self.start = start
        if end is not None:
            self.end = end
        else:
            self.end = start

    def length(self):
        return self.end - self.start + 1


class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        current = (nums[0], 1)  # ('result of ANDs', number of items)
        longest = current

        for i in range(1, len(nums)):
            current_and = current[0] & nums[i]
            if (nums[i] > current[0] and nums[i] > current_and) or current[0] > current_and:
                current = (nums[i], 1)
            else:  # current.n & nums[i] >= current.n
                current = (current[0] & nums[i], current[1] + 1)
            if current[0] > longest[0]:
                longest = current
            if current[0] == longest[0] and current[1] > longest[1]:
                longest = current
        return longest[1]

703