Maximum OR

You are given a 0-indexed integer array nums of length n and an integer k. In an operation, you can choose an element and multiply it by 2.

Return the maximum possible value of nums[0] | nums[1] | ... | nums[n - 1] that can be obtained after applying the operation on nums at most k times.

Note that a | b denotes the bitwise or between two integers a and b.

Example 1
Inputnums = [12,9], k = 1
Output30
If we apply the operation to index 1, the new array nums is [12, 18], and the bitwise OR of 12 and 18 is 30.
Example 2
Inputnums = [8,1,2], k = 2
Output35
If we apply the operation twice on index 0, the new array is [32, 1, 2], and 32 | 1 | 2 = 35.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= 15

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate