Show problems as
Classic problem #2680MediumArrayGreedyBit ManipulationPrefix Sum

Maximum OR

Real-world version: Boosting the Channel Panel
Tracked in this browser only
Read Maximum OR on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A test bench drives a wall panel from a bank of signal generators. Generator i is described by the positive integer codes[i], and the panel lights a channel whenever at least one generator carries that channel, so the panel's reading is the bitwise OR of all the generator codes.

The bench also has a tray of identical doubler modules. Fitting a doubler onto a generator replaces that generator's code with twice its current value; fitting a second doubler onto the same generator doubles it again. You may fit at most boosts doublers in total, spread across the generators however you like, including several on one generator or none at all.

Return the largest panel reading you can obtain.

Examples

Example 1

Input
codes = [12, 9], boosts = 1
Output
30

Fitting the doubler to the generator coded 9 turns it into 18, and 18 OR 12 is 30.

Example 2

Input
codes = [7], boosts = 3
Output
56

The lone generator absorbs all three doublers, taking 7 to 14, then 28, then 56.

Example 3

Input
codes = [1, 2, 4, 8], boosts = 1
Output
23

Doubling the generator coded 8 gives 16, and 16 OR 4 OR 2 OR 1 is 23.

Constraints

  • 1 <= codes.length <= 10^5
  • 1 <= codes[i] <= 10^9
  • 1 <= boosts <= 15

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def best_panel_reading(codes: list[int], boosts: int) -> int:
Java
public long bestPanelReading(int[] codes, int boosts)