Longest Nice Subarray - Problem
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: subarrays of length 1 are always considered nice.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,3,8,48,10]
›
Output:
3
💡 Note:
The longest nice subarray is [3,8,48] with length 3. Each pair has AND = 0: 3&8=0, 3&48=0, 8&48=0.
Example 2 — Single Element
$
Input:
nums = [3,1,5,11,13]
›
Output:
1
💡 Note:
No pair of elements has AND = 0, so the longest nice subarray has length 1 (any single element).
Example 3 — All Elements Nice
$
Input:
nums = [1,2,4]
›
Output:
3
💡 Note:
All elements have disjoint bits: 1&2=0, 1&4=0, 2&4=0. The entire array is nice with length 3.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code