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.
Example 1:
Input: nums = [1,3,8,48,10] Output: 3 Explanation: 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.
Example 2:
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 <= 1051 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is like trying out every single possibility, no matter how long it takes. For this particular puzzle, we examine every possible consecutive set of numbers to see if it meets our condition.
Here's how the algorithm would work step-by-step:
def longest_nice_subarray_brute_force(numbers):
longest_subarray_length = 0
for start_index in range(len(numbers)):
for end_index in range(start_index, len(numbers)):
current_subarray = numbers[start_index:end_index + 1]
# Check if the current subarray is 'nice'.
if is_nice(current_subarray):
# Update the longest length if needed.
longest_subarray_length = max(
longest_subarray_length, len(current_subarray)
)
return longest_subarray_length
def is_nice(subarray):
if not subarray:
return True
bitwise_and = subarray[0]
for i in range(1, len(subarray)):
bitwise_and &= subarray[i]
# If bitwise AND is zero, the subarray is nice.
if bitwise_and != 0:
return False
seen_numbers = set()
for number in subarray:
# Numbers must be unique to be nice.
if number in seen_numbers:
return False
seen_numbers.add(number)
return TrueThe core idea is to maintain a "window" of numbers and slide it across the input. We keep track of the numbers currently in the window such that their bitwise AND is zero. This window grows and shrinks to maximize its length while maintaining the zero AND condition.
Here's how the algorithm would work step-by-step:
def longestNiceSubarray(numbers) -> int:
max_length = 0
current_and = 0
left_index = 0
for right_index, current_number in enumerate(numbers):
# Expand the window and update bitwise AND.
current_and |= current_number
# Shrink the window until bitwise AND becomes zero.
while current_and != 0:
current_and ^= numbers[left_index]
left_index += 1
# This ensures the AND is calculated only for the nice subarray.
max_length = max(max_length, right_index - left_index + 1)
return max_length| Case | How to Handle |
|---|---|
| Empty input array | Return 0 since no subarray can be formed. |
| Array with a single element | Return 1, as a single element subarray is always 'nice'. |
| Array with all elements being 0 | The entire array is a nice subarray, so return the array's length. |
| Array with elements that individually satisfy the 'nice' property but combined do not | The sliding window approach will dynamically adjust the window size to maintain the 'nice' property. |
| Array contains large integers that could potentially lead to integer overflow during bitwise AND operations if not handled carefully | Use appropriate data types to avoid overflow, or perform the check modularly if the problem allows for it, although bitwise AND inherently mitigates overflow risk as values are always reduced. |
| Array with very long sequence where the running bitwise AND is always zero until the very end | The solution's sliding window should still efficiently identify the longest sequence, moving the left pointer to adjust the subarray. |
| Maximum array size close to memory limits | The solution should use space efficiently (O(1) space for bitwise AND solution) to avoid memory issues. |
| All elements in the input array are the maximum integer value | Bitwise AND operations between any numbers should still return a valid result because the numbers are integers and bitwise operations are defined on integers. |