Longest Nice Subarray

You are given an array nums consisting of positive integers.

We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0.

Return the length of the longest nice subarray.

A subarray is a contiguous part of an array.

Note that subarrays of length 1 are always considered nice.

Example 1
Inputnums = [1,3,8,48,10]
Output3
The longest nice subarray is [3, 8, 48], whose pairwise bitwise AND values are all 0, and no longer nice subarray can be obtained.
Example 2
Inputnums = [3,1,5,11,13]
Output1
The length of the longest nice subarray is 1, and any subarray of length 1 can be chosen.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate