Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 15Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy · Bit Manipulation
[12,9] 1
[8,1,2] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2680: Maximum OR
class Solution {
public long maximumOr(int[] nums, int k) {
int n = nums.length;
long[] suf = new long[n + 1];
for (int i = n - 1; i >= 0; --i) {
suf[i] = suf[i + 1] | nums[i];
}
long ans = 0, pre = 0;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, pre | (1L * nums[i] << k) | suf[i + 1]);
pre |= nums[i];
}
return ans;
}
}
// Accepted solution for LeetCode #2680: Maximum OR
func maximumOr(nums []int, k int) int64 {
n := len(nums)
suf := make([]int, n+1)
for i := n - 1; i >= 0; i-- {
suf[i] = suf[i+1] | nums[i]
}
ans, pre := 0, 0
for i, x := range nums {
ans = max(ans, pre|(nums[i]<<k)|suf[i+1])
pre |= x
}
return int64(ans)
}
# Accepted solution for LeetCode #2680: Maximum OR
class Solution:
def maximumOr(self, nums: List[int], k: int) -> int:
n = len(nums)
suf = [0] * (n + 1)
for i in range(n - 1, -1, -1):
suf[i] = suf[i + 1] | nums[i]
ans = pre = 0
for i, x in enumerate(nums):
ans = max(ans, pre | (x << k) | suf[i + 1])
pre |= x
return ans
// Accepted solution for LeetCode #2680: Maximum OR
impl Solution {
pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
let n = nums.len();
let mut suf = vec![0; n + 1];
for i in (0..n).rev() {
suf[i] = suf[i + 1] | (nums[i] as i64);
}
let mut ans = 0i64;
let mut pre = 0i64;
let k64 = k as i64;
for i in 0..n {
ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]);
pre |= nums[i] as i64;
}
ans
}
}
// Accepted solution for LeetCode #2680: Maximum OR
function maximumOr(nums: number[], k: number): number {
const n = nums.length;
const suf: bigint[] = Array(n + 1).fill(0n);
for (let i = n - 1; i >= 0; i--) {
suf[i] = suf[i + 1] | BigInt(nums[i]);
}
let [ans, pre] = [0, 0n];
for (let i = 0; i < n; i++) {
ans = Math.max(Number(ans), Number(pre | (BigInt(nums[i]) << BigInt(k)) | suf[i + 1]));
pre |= BigInt(nums[i]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.