All problems
1118MediumArrayBacktrackingBit ManipulationEnumeration

Groups of Relays That Reach the Widest

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2044Count Number of Maximum Bitwise-OR Subsets

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 rack holds relays, and relays[i] is a whole number saying which lines relay i closes: the lines are the bits of that number.

Picking a group of one or more relays closes every line that any relay in the group closes, so the group's reach is the bitwise OR of the numbers in it.

Let the widest reach be the largest reach any group can manage. Return how many groups reach it. Two groups count apart when they use different relays, even if their reaches match.

Examples

Example 1

Input
relays = [1, 1]
Output
3

Both relays close the same single line, so the widest reach is that line and all three non-empty groups manage it.

Example 2

Input
relays = [4, 1, 2, 3]
Output
5

Together the relays close three lines. Only one relay closes the third of them, so it belongs to every group that reaches the widest, and the other three relays supply the remaining two lines in five ways.

Example 3

Input
relays = [1, 2, 4, 8]
Output
1

Each relay closes a line no other relay touches, so only the group holding all four reaches the widest.

Constraints

  • 1 <= relays.length <= 16
  • 1 <= relays[i] <= 10^5

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 count_max_or_subsets(relays: list[int]) -> int:
Java
public int countMaxOrSubsets(int[] relays)
September 7
Apply