Given an integer array nums, a subarray is called nice if for every pair of distinct indices i and j within that subarray, the bitwise AND of the two values is equal to 0, meaning nums[i] & nums[j] == 0.
Return the maximum possible length of a nice subarray of nums.
Note: A subarray is a contiguous segment of nums.
Constraints:
Input: nums = [1, 3, 8, 48, 10]-List[int]
Output: 3-int
Explanation: The longest nice subarray is [3, 8, 48], since 3 & 8 = 0 (0011 & 1000 = 0000), 3 & 48 = 0 (0011 & 110000 = 000000), and 8 & 48 = 0 (1000 & 110000 = 000000). Any attempt to extend it (e.g., adding 10) breaks niceness because 8 & 10 = 8 != 0.
Input: nums = [1, 2, 4, 8]-List[int]
Output: 4-int
Explanation: All numbers have disjoint set bits, so every pair ANDs to 0, and the entire array is a nice subarray.
Input: nums = [5, 5, 5]-List[int]
Output: 1-int
Explanation: 5 & 5 = 5 != 0, so no subarray of length ≥ 2 can be nice, and the best you can do is any single element.
Input: nums = [2, 3, 1, 8]-List[int]
Output: 2-int
Explanation: [2, 3] is not nice because 2 & 3 = 2 != 0, and [3, 1] is not nice because 3 & 1 = 1 != 0, but [1, 8] is nice because 1 & 8 = 0, so the maximum nice subarray length is 2.
Input: nums = [16, 1, 2, 4, 7]-List[int]
Output: 4-int
Explanation: The subarray [16, 1, 2, 4] is nice since all bits are non-overlapping: 16 (10000) shares no set bits with 1 (00001), 2 (00010), or 4 (00100), and 1, 2, and 4 are also pairwise disjoint. Adding 7 (00111) breaks niceness because it overlaps with 1, 2, and 4 (e.g., 7 & 4 = 4).
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: