1396. Longest Subarray with AND Greater Than Zero

HardBit ManipulationBit MathSliding WindowAND

Given an array of non-negative integers, find the length of the longest contiguous subarray whose bitwise AND is greater than zero. A positive AND means all elements of the subarray share at least one common set bit.

Input: A JSON object {"nums": [<non-negative integers>]}.

Output: Return the length of the longest subarray with AND greater than 0.

Examples

Example 1
Input: {"nums":[7,3,1,2,6,6]}
Output: 3
Explanation: The last three elements share bit 1, a run of length 3.
Example 2
Input: {"nums":[1,1,1,1]}
Output: 4
Explanation: Every element shares bit 0 -> length 4.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →