---
# System prepended metadata

title: 2419. Longest Subarray With Maximum Bitwise AND
tags: [Leetcode, Bit Manipulation, Medium]

---

# 2419. Longest Subarray With Maximum Bitwise AND
###### tags: `Leetcode` `Medium` `Bit Manipulation`
Link: https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/
## 思路
Max bitwise **AND** subarray must include maximum element(s) **ONLY**. Therefore, we only need to find the longest subarray including max only.
## Code
```java=
class Solution {
    public int longestSubarray(int[] nums) {
        int max=0, longest=0, curr=0;
        for(int num:nums){
            if(num==max){
                curr++;
                longest = Math.max(longest, curr);
            }
            else if(num>max){
                max = num;
                curr = longest = 1;
            }
            else{
                curr=0;
            }
        }
        return longest;
    }
}
```