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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums and two integers k and m.
You may perform at most k operations. In one operation, you may choose any index i and increase nums[i] by 1.
Return an integer denoting the maximum possible bitwise AND of any subset of size m after performing up to k operations optimally.
Example 1:
Input: nums = [3,1,2], k = 8, m = 2
Output: 6
Explanation:
m = 2. Choose indices [0, 2].nums[0] = 3 to 6 using 3 operations, and increase nums[2] = 2 to 6 using 4 operations.k = 8.[6, 6], and their bitwise AND is 6, which is the maximum possible.Example 2:
Input: nums = [1,2,8,4], k = 7, m = 3
Output: 4
Explanation:
m = 3. Choose indices [0, 1, 3].nums[0] = 1 to 4 using 3 operations, increase nums[1] = 2 to 4 using 2 operations, and keep nums[3] = 4.k = 7.[4, 4, 4], and their bitwise AND is 4, which is the maximum possible.Example 3:
Input: nums = [1,1], k = 3, m = 2
Output: 2
Explanation:
m = 2. Choose indices [0, 1].k = 3.[2, 2], and their bitwise AND is 2, which is the maximum possible.Constraints:
1 <= n == nums.length <= 5 * 1041 <= nums[i] <= 1091 <= k <= 1091 <= m <= nProblem summary: You are given an integer array nums and two integers k and m. You may perform at most k operations. In one operation, you may choose any index i and increase nums[i] by 1. Return an integer denoting the maximum possible bitwise AND of any subset of size m after performing up to k operations optimally.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy · Bit Manipulation
[3,1,2] 8 2
[1,2,8,4] 7 3
[1,1] 3 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
class Solution {
public int maximumAND(int[] nums, int k, int m) {
int max = 0;
for (int x : nums) {
max = Math.max(max, x);
}
max += k;
int mx = 32 - Integer.numberOfLeadingZeros(max);
int n = nums.length;
int ans = 0;
int[] cost = new int[n];
for (int bit = mx - 1; bit >= 0; bit--) {
int target = ans | (1 << bit);
for (int i = 0; i < n; i++) {
int x = nums[i];
int diff = target & ~x;
int j = diff == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(diff);
int mask = (1 << j) - 1;
cost[i] = (target & mask) - (x & mask);
}
Arrays.sort(cost);
long sum = 0;
for (int i = 0; i < m; i++) {
sum += cost[i];
}
if (sum <= k) {
ans = target;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
func maximumAND(nums []int, k int, m int) int {
mx := bits.Len(uint(slices.Max(nums) + k))
ans := 0
cost := make([]int, len(nums))
for bit := mx - 1; bit >= 0; bit-- {
target := ans | (1 << bit)
for i, x := range nums {
j := bits.Len(uint(target & ^x))
mask := (1 << j) - 1
cost[i] = (target & mask) - (x & mask)
}
sort.Ints(cost)
sum := 0
for i := 0; i < m; i++ {
sum += cost[i]
}
if sum <= k {
ans = target
}
}
return ans
}
# Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
class Solution:
def maximumAND(self, nums: List[int], k: int, m: int) -> int:
mx = (max(nums) + k).bit_length()
ans = 0
cost = [0] * len(nums)
for bit in range(mx - 1, -1, -1):
target = ans | (1 << bit)
for i, x in enumerate(nums):
j = (target & ~x).bit_length()
mask = (1 << j) - 1
cost[i] = (target & mask) - (x & mask)
cost.sort()
if sum(cost[:m]) <= k:
ans = target
return ans
// Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
// class Solution {
// public int maximumAND(int[] nums, int k, int m) {
// int max = 0;
// for (int x : nums) {
// max = Math.max(max, x);
// }
// max += k;
//
// int mx = 32 - Integer.numberOfLeadingZeros(max);
// int n = nums.length;
//
// int ans = 0;
// int[] cost = new int[n];
//
// for (int bit = mx - 1; bit >= 0; bit--) {
// int target = ans | (1 << bit);
// for (int i = 0; i < n; i++) {
// int x = nums[i];
// int diff = target & ~x;
// int j = diff == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(diff);
// int mask = (1 << j) - 1;
// cost[i] = (target & mask) - (x & mask);
// }
// Arrays.sort(cost);
// long sum = 0;
// for (int i = 0; i < m; i++) {
// sum += cost[i];
// }
// if (sum <= k) {
// ans = target;
// }
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3806: Maximum Bitwise AND After Increment Operations
function maximumAND(nums: number[], k: number, m: number): number {
const mx = 32 - Math.clz32(Math.max(...nums) + k);
let ans = 0;
const n = nums.length;
const cost = new Array(n);
for (let bit = mx - 1; bit >= 0; bit--) {
let target = ans | (1 << bit);
for (let i = 0; i < n; i++) {
const x = nums[i];
const diff = target & ~x;
const j = diff === 0 ? 0 : 32 - Math.clz32(diff);
const mask = (1 << j) - 1;
cost[i] = (target & mask) - (x & mask);
}
cost.sort((a, b) => a - b);
let sum = 0;
for (let i = 0; i < m; i++) {
sum += cost[i];
}
if (sum <= k) {
ans = target;
}
}
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.