Longest Nice Subarray

Medium Topics Company Tags

A contiguous subarray is nice when the bitwise AND of every pair of values at different positions is zero. Return the length of the longest nice subarray of nums. A one-element subarray always qualifies.

Example 1:

Input: nums = [1,2,4,1]

Output: 3

Explanation: The first three entries, [1,2,4], use different set bits, so every pair has AND 0. The full array repeats the bit in 1 and is not nice.

Example 2:

Input: nums = [7,7]

Output: 1

Explanation: The pair has AND 7, so both entries cannot be kept together. Either single entry forms a nice subarray of length 1.

Example 3:

Input: nums = [8,1,2,4]

Output: 4

Explanation: Each entry has a different single set bit. Every pair has AND 0, so the entire length-4 array is nice.

Constraints:

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.