Skip to content
Decoder

Longest Nice Subarray

Medium

Problem

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.

Example 1

Input
nums = [1, 3, 8, 48, 10]
Output
3
Why
[3, 8, 48]

Example 2

Input
nums = [3, 1, 5, 11, 13]
Output
1

Example 3

Input
nums = [1, 2, 4, 8, 16]
Output
5

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.