Problem · Array

Get Max Or Sum

Learn this problem
MediumMicrosoft logoMicrosoftOA
See Microsoft hiring insights

Problem statement

For an array of n positive integers, arr, perform the following operation up to k times:

  1. Choose an index i such that 0 ≤ i < n.
  2. Replace arr[i] with arr[i] × 2.

The or-sum is the bitwise OR of all elements in the final array. Return the maximum possible or-sum.

Function

getMaxOrSum(arr: int[], k: int) → long

Examples

Example 1

arr = [12, 9]k = 1return = 30

Doubling 12 produces [24, 9], whose or-sum is 25. Doubling 9 produces [12, 18], whose or-sum is 30. Therefore, the maximum possible or-sum is 30.

Constraints

  • 1 ≤ n ≤ 100,000
  • 1 ≤ arr[i] ≤ 1,000,000
  • 1 ≤ k ≤ 11

More Microsoft problems

drafts saved locally
public long getMaxOrSum(int[] arr, int k) {
  // write your code here
}
arr[12, 9]
k1
expected30
checking account