Problem Description
Given an array of positive integers, find the length of the longest contiguous subarray such that for every pair of distinct elements in the subarray, the bitwise AND is equal to 0.
Key Insights
- The subarray is "nice" if no two numbers have any overlapping set bits (i.e. the bitwise AND of any two different elements is 0).
- A sliding window approach is natural since the subarray must be contiguous.
- Use a bitmask to keep track of the combined bits included in the current window.
- When a new number conflicts (shares a common bit) with the current bitmask, slide the left end of the window until the conflict is resolved.
Space and Time Complexity
Time Complexity: O(n) - each element is processed at most twice (once when added and once when removed). Space Complexity: O(1) - only a few extra variables (bitmask, pointers) are used independent of input size.
Solution
We use the sliding window algorithm where two pointers (left and right) are maintained representing the current subarray. A bitmask (currBitmask) tracks the union of bits present in all numbers in the current window. For every new number at the right pointer, check if its bits conflict (i.e. there is any bit that is already set in currBitmask). If there is a conflict, move the left pointer rightwards, removing numbers from the bitmask until there is no conflict. Once the new number is inserted into the window (by updating the bitmask), update the maximum window length. This ensures that the current window always represents a "nice" subarray.