Skip to content

Longest Nice Subarray

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

A subarray is nice if the bitwise AND of every pair of elements at different positions in it is 0. In other words, no bit is set in more than one of its elements. Given an array of positive integers nums, return the length of the longest nice subarray. A subarray of length 1 is always nice.

nums = [1, 3, 8, 48, 10]    →  3   ([3, 8, 48])
nums = [3, 1, 5, 11, 13]    →  1
nums = [1, 2, 4, 8, 16]     →  5

Constraints

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