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:
Input: nums = [12,9], k = 1 Output: 30 Explanation: If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
Example 2:
Input: nums = [8,1,2], k = 2 Output: 35 Explanation: If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 15When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
When facing a problem where we want to find the absolute maximum combined value using a specific number of allowed adjustments, the brute force approach is the ultimate try-everything method. We simply examine every single combination of allowed changes, calculate the resulting combined value for each one, and keep track of the largest result we encounter.
Here's how the algorithm would work step-by-step:
def maximum_or_brute_force(numbers, allowed_operations, multiplier):
# We start with the base combined value with zero modifications applied
current_maximum = 0
for number in numbers:
current_maximum |= number
def generate_all_combinations(index, operations_left, current_numbers):
nonlocal current_maximum
if index == len(numbers) or operations_left == 0:
combined_value = 0
for number in current_numbers:
combined_value |= number
if combined_value > current_maximum:
current_maximum = combined_value
return
# We explore the branch where we apply our multiplier to the current element
modified_numbers = list(current_numbers)
modified_numbers[index] *= (multiplier ** operations_left)
generate_all_combinations(index + 1, 0, modified_numbers)
# We explore the branch where we skip modifying the current element
generate_all_combinations(index + 1, operations_left, current_numbers)
# We test every valid combination of operations to find the global maximum
for start_index in range(len(numbers)):
generate_all_combinations(0, allowed_operations, list(numbers))
return current_maximumTo find the greatest possible bitwise combination after multiplying one chosen number by a power of two, we want to make the largest number possible as powerful as we can. The winning strategy is to figure out which number can contribute the most unique bits to the final combined total when shifted left.
Here's how the algorithm would work step-by-step:
def maximum_or(number_list, shift_multiplier):
array_length = len(number_list)
prefix_bitwise_or = [0] * array_length
suffix_bitwise_or = [0] * array_length
# We precalculate cumulative OR values from the left to know existing bits.
current_prefix_or = 0
for index in range(array_length):
prefix_bitwise_or[index] = current_prefix_or
current_prefix_or |= number_list[index]
# We precalculate cumulative OR values from the right to complete our bit map.
current_suffix_or = 0
for index in range(array_length - 1, -1, -1):
suffix_bitwise_or[index] = current_suffix_or
current_suffix_or |= number_list[index]
maximum_result = 0
multiplier_power = 1 << shift_multiplier
# We test shifting each number to find which one yields the greatest OR total.
for index in range(array_length):
boosted_value = number_list[index] * multiplier_power
combined_bitwise_or = prefix_bitwise_or[index] | boosted_value | suffix_bitwise_or[index]
if combined_bitwise_or > maximum_result:
maximum_result = combined_bitwise_or
return maximum_result| Case | How to Handle |
|---|---|
| Zero operations allowed (k = 0) | The algorithm simply computes the bitwise OR of the original array without modifying any elements. |
| Single element array | The element is multiplied by two k times, and the resulting single value is returned as the maximum OR. |
| Array containing only zeros | Multiplying zero by two yields zero, so the result remains zero regardless of the number of operations. |
| Large number of operations (k is very large) | Shifting any element by k positions will still fit within standard integer limits if managed carefully or using 64-bit integers. |
| All elements are equal | The greedy approach will focus operations on a single chosen element to maximize its bit contributions relative to others. |
| Integer overflow from massive bit shifts | Using 64-bit integer types ensures that multiplying by two k times does not cause overflow issues during bitwise operations. |
| High constraint on array size and value magnitude | A precomputed prefix and suffix bitwise OR array approach ensures optimal linear or near-linear time complexity. |