All problems
1009MediumArrayBit ManipulationSliding Window

The Longest Run of Masks Sharing No Channel

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2401Longest Nice Subarray

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A list of channel masks reads masks. Each mask is a positive whole number, and the bits of its binary form say which channels that mask claims.

A run of neighbouring masks is clean when no channel is claimed by two of its masks: for every pair in the run, no bit is set in both.

Return the length of the longest clean run. A run holding a single mask is always clean.

Examples

Example 1

Input
masks = [2, 4, 8, 3]
Output
3

The first three masks each claim a single channel of their own, so they form a clean run of three. The last mask claims the two lowest channels, which clashes with the first mask, but the last three masks are clean together as well, so three is the best either way.

Example 2

Input
masks = [7, 7, 7]
Output
1

All three masks claim the same channels, so no two of them sit in a run together and a single mask is the longest clean run.

Example 3

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

Every mask claims a channel no other one touches, so the whole list is clean.

Constraints

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

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_nice_subarray(masks: list[int]) -> int:
Java
public int longestNiceSubarray(int[] masks)
September 7
Apply