1013. Bit-Disjoint Stretch
A radio station tags each clip with a bit-mask nums[i], where every set bit stands for one frequency band the clip occupies. Two clips can play back to back without interference only if they share no band, that is, their bitwise AND is zero.
Find the length of the longest contiguous stretch of clips in which every pair of clips (not only neighbours) is interference-free. A single clip always counts, and a clip with mask 0 interferes with nothing.
Return that length. Expected solution: O(n) time and O(1) extra space.
Example 1
- Input:
- nums = [11,4,16,3,0,8,32]
- Output:
- 6
- Explanation:
The stretch 4, 16, 3, 0, 8, 32 has no two values sharing a set bit (only 11 clashes, with 3 and 8), so its length 6 is the best.
Example 2
- Input:
- nums = [10,5,2,0,5,1]
- Output:
- 3
- Explanation:
Windows such as 10, 5 or 2, 0, 5 are valid, but extending to 5, 2, 0, 5 repeats bit 0 twice, and 10 with 2 share a bit, so the longest valid stretch has 3 elements.
Constraints
1 ≤ n ≤ 105
0 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(1)