#2419

Longest Subarray With Maximum Bitwise AND

specialist · 635 · lc medium +30 · verified · 65.4% accepted · 1,317 likes · top 70%

Description

You are given an integer array nums of size n. Let k be the maximum value that any bitwise AND of a subarray of nums can achieve.

Return the length of the longest subarray whose bitwise AND equals k.

Example 1:

Input: nums = [1,2,3,3,2,2]
Output: 2
Explanation:
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.

Example 2:

Input: nums = [1,2,3,4]
Output: 1
Explanation:
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.

Code

1
2
3