Bitwise ORs of Subarrays
Real-world version: How Many Different Merged Masks AppearThe statement here is our own: the same inputs, outputs and technique.
A list of channel masks reads masks, each a whole number whose bits say which channels it claims.
Merging a run of neighbouring masks claims every channel any of them claims, so the merged mask has a bit set exactly where at least one mask of the run has it set.
Return how many different merged masks arise over all the runs of one or more neighbouring masks.
Examples
Example 1
- Input
- masks = [1, 2, 3]
- Output
- 3
The single masks give 1, 2 and 3. Merging 1 with 2 gives 3, merging 2 with 3 gives 3, and merging all three gives 3, so only 1, 2 and 3 ever arise.
Example 2
- Input
- masks = [5, 5, 5]
- Output
- 1
Every mask claims the same channels, so every run merges to the same mask.
Example 3
- Input
- masks = [0, 0, 0]
- Output
- 1
No mask claims any channel, so every run merges to nothing.
Constraints
- 1 <= masks.length <= 5 * 10^4
- 0 <= masks[i] <= 10^9
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.
def subarray_bitwise_o_rs(masks: list[int]) -> int:public int subarrayBitwiseORs(int[] masks)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Beautiful ArrangementReal-world version: Divisible Crate PlacementsMedium
- Partition to K Equal Sum SubsetsReal-world version: Dealing Counterweights Onto HoistsMedium
- Shopping OffersReal-world version: Cheapest Basket at the Fastener CounterMedium
- Matchsticks to SquareReal-world version: Fencing a Square From Every RodMedium