2730. Maximum OR
My accepted Python solution to LeetCode problem 2730, Maximum OR, running in 130ms.
- Difficulty: Medium
- Python
- Runtime 130ms
- Memory 30.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 130ms, memory 30.8MB, accepted 2025-12-31.
class Solution:
def maximumOr(self, nums: List[int], k: int) -> int:
n = len(nums)
# Suffix OR: suffix[i] = OR of nums[i+1:]
suffix = [0] * (n + 1)
for i in range(n - 1, -1, -1):
suffix[i] = suffix[i + 1] | nums[i]
# Try multiplying each element by 2^k
result = 0
prefix = 0
for i in range(n):
# Multiply nums[i] by 2^k
current = prefix | (nums[i] << k) | suffix[i + 1]
result = max(result, current)
prefix |= nums[i]
return result