Magicsheet logo

Maximum OR

Medium
39.6%
Updated 6/1/2025

Maximum OR

What is this problem about?

The Maximum OR coding problem gives you an array nums of non-negative integers. You are allowed to perform at most k operations. In each operation, you can choose any element nums[i] and multiply it by 2. Your goal is to maximize the bitwise OR sum of all elements in the array after performing at most k operations.

Why is this asked in interviews?

Microsoft and DE Shaw use this problem to test a candidate's understanding of bit manipulation, greedy algorithms, and prefix/suffix sums. The problem requires finding the optimal number of multiplications for a single element to maximize the overall bitwise OR.

Algorithmic pattern used

Greedy + Prefix/Suffix OR: The bitwise OR operation means that if any bit is set in any number, it will be set in the final OR sum. To maximize the OR sum, we want to set as many high-value bits as possible. Multiplying a number by 2 is equivalent to left-shifting its binary representation by 1 bit (nums[i] * 2 = nums[i] << 1). Performing k operations on a single number nums[i] means nums[i] << k. The crucial insight is that it's always optimal to apply all k multiplications to a single element. Why? Because applying multiple shifts to one number allows its bits to "reach" higher bit positions, potentially setting a bit that no other number can reach, thereby maximizing the total OR sum. Distributing shifts (e.g., shifting two numbers by k/2 each) would result in smaller individual shifts, likely leading to a smaller overall OR sum.

So, the problem becomes: for each element nums[i], calculate what the total OR sum would be if nums[i] is multiplied by 2^k, and all other elements nums[j] (j != i) remain unchanged. We need an efficient way to calculate (nums[0] | ... | (nums[i] << k) | ... | nums[n-1]).

Algorithm:

  1. Precompute Prefix ORs: Create an array prefix_OR[i] where prefix_OR[i] = nums[0] | nums[1] | ... | nums[i]. prefix_OR[-1] can be considered 0.
  2. Precompute Suffix ORs: Create an array suffix_OR[i] where suffix_OR[i] = nums[i] | nums[i+1] | ... | nums[n-1]. suffix_OR[n] can be considered 0.
  3. Iterate and Calculate Max OR: Initialize max_OR_sum = 0. For each index i from 0 to n-1: a. Calculate the OR sum of elements before i: left_OR = (i > 0) ? prefix_OR[i-1] : 0. b. Calculate the OR sum of elements after i: right_OR = (i < n-1) ? suffix_OR[i+1] : 0. c. Calculate current_num_shifted = nums[i] << k. d. current_total_OR = left_OR | current_num_shifted | right_OR. e. max_OR_sum = max(max_OR_sum, current_total_OR).
  4. Return max_OR_sum.

Time Complexity: O(N) for prefix/suffix ORs, and O(N) for the final iteration. Total O(N).

Example explanation

nums = [1, 2, 4, 8], k = 2.

  1. Prefix ORs: prefix_OR[0] = 1 prefix_OR[1] = 1 | 2 = 3 prefix_OR[2] = 3 | 4 = 7 prefix_OR[3] = 7 | 8 = 15 prefix_OR = [1, 3, 7, 15]

  2. Suffix ORs: suffix_OR[3] = 8 suffix_OR[2] = 4 | 8 = 12 suffix_OR[1] = 2 | 12 = 14 suffix_OR[0] = 1 | 14 = 15 suffix_OR = [15, 14, 12, 8]

  3. Iterate and calculate max OR:

    • i = 0 (nums[0]=1): left_OR = 0 (no elements before) right_OR = suffix_OR[1] = 14 (OR of nums[1], nums[2], nums[3]) current_num_shifted = 1 << 2 = 4 current_total_OR = 0 | 4 | 14 = 14. max_OR_sum = 14.
    • i = 1 (nums[1]=2): left_OR = prefix_OR[0] = 1 right_OR = suffix_OR[2] = 12 current_num_shifted = 2 << 2 = 8 current_total_OR = 1 | 8 | 12 = 13. max_OR_sum = max(14, 13) = 14.
    • i = 2 (nums[2]=4): left_OR = prefix_OR[1] = 3 right_OR = suffix_OR[3] = 8 current_num_shifted = 4 << 2 = 16 current_total_OR = 3 | 16 | 8 = 27. max_OR_sum = max(14, 27) = 27.
    • i = 3 (nums[3]=8): left_OR = prefix_OR[2] = 7 right_OR = 0 (no elements after) current_num_shifted = 8 << 2 = 32 current_total_OR = 7 | 32 | 0 = 39. max_OR_sum = max(27, 39) = 39.

Result: 39.

Common mistakes candidates make

  • Not realizing optimality of concentrating shifts: Believing that distributing k operations across multiple elements might be better.
  • Inefficient calculation of total OR: Re-computing the OR sum for each i without prefix/suffix ORs leads to O(N^2) complexity.
  • Bit manipulation errors: Incorrect shift amounts or logical OR operations.

Interview preparation tip

For the Array Bit Manipulation Greedy Prefix Sum interview pattern, problems involving maximizing bitwise OR (or AND) often benefit from greedy choices and clever precomputation with prefix/suffix arrays. Understand how bit shifts affect the value and how OR operations combine bits.

Similar Questions