Problem Description
Given an integer array nums, find the longest contiguous subarray whose bitwise AND equals the maximum value obtainable by any subarray. The maximum bitwise AND is achieved by a subarray containing only occurrences of the maximum element in nums. Return the length of the longest such subarray.
Key Insights
- The bitwise AND of any subarray can never exceed the minimum element in that subarray.
- For the bitwise AND to be maximum overall, the subarray must consist solely of the maximum element in nums.
- The problem reduces to finding the longest contiguous sequence of the maximum element in the array.
Space and Time Complexity
Time Complexity: O(n) – We traverse the array once. Space Complexity: O(1) – Only constant extra space is used.
Solution
- Find the maximum element in the given array nums.
- Iterate through nums to compute the length of each contiguous subarray that contains only this maximum element.
- Track and return the length of the longest such subarray.
Data Structures/Algorithms used:
- Single pass iteration for both finding max value and counting contiguous subarrays.
- Constant extra space using variables to track maximum value and current streak length.