Description

You are given an array nums of positive integers. A subarray is nice if the bitwise AND of every pair of elements that are in different positions is 0. Return the length of the longest nice subarray.

Examples

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

[3,8,48] is nice.

Input:nums = [2, 4, 16, 32, 1]
Output:4
Explanation:

The subarray [2, 4, 16, 32] is nice because each number uses different bit positions: 2 (10₂), 4 (100₂), 16 (10000₂), 32 (100000₂). Since no two numbers share any set bits, their pairwise AND operations all equal 0.

Input:nums = [5, 5, 5, 5]
Output:1
Explanation:

Since all elements are identical (5 = 101₂), any pair from different positions will have AND result of 5 (not 0). Therefore, the longest nice subarray can only contain one element, giving us a length of 1.

Constraints

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!