<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>

<p>We call a subarray of <code>nums</code> <strong>nice</strong> if the bitwise <strong>AND</strong> of every pair of elements that are in <strong>different</strong> positions in the subarray is equal to <code>0</code>.</p>

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

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p><strong>Note</strong> that subarrays of length <code>1</code> are always considered nice.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,8,48,10]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:
- 3 AND 8 = 0.
- 3 AND 48 = 0.
- 8 AND 48 = 0.
It can be proven that no longer nice subarray can be obtained, so we return 3.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,5,11,13]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.
</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>9</sup></code></li>
</ul>

from typing import List


class Solution:
    # Second iteration, change the brute force while condition and optimize
    def longestNiceSubarray(self, nums: List[int]) -> int:
    
            r = 1
            window = [nums[0]]
            longest = 1
    
            while r < len(nums):
                currentElement = nums[r]
                print(window)
    
                # Inside our While loop to pop left of window
    			# while our Window is invalid
    			# (window is invalid when current element we want to add & any existing element is not equal 0)
    			# pop window left side until it becomes valid again
                while any( x & currentElement != 0 for x in window):
                    window.pop(0)
    
                # This is our window valid condition
                window.append(currentElement)
                longest = max(longest,len(window))
                r+=1
    
            return longest
    

    # First iteration, we brute force and just implement sliding window. 
    # def longestNiceSubarray(self, nums: List[int]) -> int:

    #     l = 0
    #     r = 1

    #     window = [nums[0]]
    #     longest = 1

    #     orCount = nums[0]
    #     while r < len(nums):
    #         rEle = nums[r]
    #         print(window)

    #         # See here again, we can inverse the situation, where WHILE loop is invalid
    #         while any( x & rEle != 0 for x in window):
    #             # print(window)
    #             window.pop(0)
    #         # And only here is valid situation
    #         window.append(rEle)
    #         longest = max(longest,len(window))
    #         r+=1

    #     return longest
        
print(
    Solution().longestNiceSubarray(
[3,8,48]
    )
)
884