You are given an array nums
consisting of positive integers.
We call a subarray of nums
nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0
.
Return the length of the longest nice subarray.
A subarray is a contiguous part of an array.
Note that subarrays of length 1
are always considered nice.
For example: Input: nums = [1,3,8,48,10] Output: 3 Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:
Another Example: Input: nums = [3,1,5,11,13] Output: 1 Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.
Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
## Longest Nice Subarray
This problem requires us to find the longest subarray in a given array of positive integers such that the bitwise AND of every pair of elements at different positions in the subarray is equal to 0.
### 1. Brute Force Approach
The most straightforward approach is to generate all possible subarrays and check if each subarray is "nice" according to the problem's definition. For each subarray, we iterate through all pairs of elements and compute their bitwise AND. If the AND of any pair is not 0, the subarray is not nice. We keep track of the longest nice subarray found so far.
```python
def is_nice_subarray(subarray):
for i in range(len(subarray)):
for j in range(i + 1, len(subarray)):
if subarray[i] & subarray[j] != 0:
return False
return True
def longest_nice_subarray_brute_force(nums):
max_len = 0
for i in range(len(nums)):
for j in range(i, len(nums)):
subarray = nums[i:j+1]
if is_nice_subarray(subarray):
max_len = max(max_len, len(subarray))
return max_len
# Example usage
nums = [1, 3, 8, 48, 10]
print(longest_nice_subarray_brute_force(nums)) # Output: 3
We can optimize this by using a sliding window. We maintain a window (subarray) and keep track of the bitwise OR of all elements in the window. We expand the window to the right. If the bitwise AND of all pairs is 0, it means the bitwise OR of all elements should have at most one set bit for each bit position. If adding a new element to the window violates this condition, we shrink the window from the left until the condition is satisfied. The maximum length of the window during this process is the length of the longest nice subarray.
def longest_nice_subarray(nums):
max_len = 0
current_or = 0
left = 0
for right in range(len(nums)):
while current_or & nums[right] != 0:
current_or ^= nums[left]
left += 1
current_or |= nums[right]
max_len = max(max_len, right - left + 1)
return max_len
# Example usage
nums = [1, 3, 8, 48, 10]
print(longest_nice_subarray(nums)) # Output: 3
current_or
, left
, and max_len
, so the space complexity is O(1).while
condition in the sliding window takes care of removing elements on the left side until the bitwise AND of the current window becomes zero.