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.
The bitwise AND of an array nums is the bitwise AND of all integers in nums.
nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.nums = [7], the bitwise AND is 7.You are given an array of positive integers candidates. Compute the bitwise AND for all possible combinations of elements in the candidates array.
Return the size of the largest combination of candidates with a bitwise AND greater than 0.
Example 1:
Input: candidates = [16,17,71,62,12,24,14] Output: 4 Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0. The size of the combination is 4. It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0. Note that more than one combination may have the largest size. For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
Example 2:
Input: candidates = [8,8] Output: 2 Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0. The size of the combination is 2, so we return 2.
Constraints:
1 <= candidates.length <= 1051 <= candidates[i] <= 107Problem summary: The bitwise AND of an array nums is the bitwise AND of all integers in nums. For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1. Also, for nums = [7], the bitwise AND is 7. You are given an array of positive integers candidates. Compute the bitwise AND for all possible combinations of elements in the candidates array. Return the size of the largest combination of candidates with a bitwise AND greater than 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
[16,17,71,62,12,24,14]
[8,8]
count-number-of-maximum-bitwise-or-subsets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2275: Largest Combination With Bitwise AND Greater Than Zero
class Solution {
public int largestCombination(int[] candidates) {
int mx = Arrays.stream(candidates).max().getAsInt();
int m = Integer.SIZE - Integer.numberOfLeadingZeros(mx);
int ans = 0;
for (int i = 0; i < m; ++i) {
int cnt = 0;
for (int x : candidates) {
cnt += x >> i & 1;
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
// Accepted solution for LeetCode #2275: Largest Combination With Bitwise AND Greater Than Zero
func largestCombination(candidates []int) (ans int) {
mx := slices.Max(candidates)
m := bits.Len(uint(mx))
for i := 0; i < m; i++ {
cnt := 0
for _, x := range candidates {
cnt += (x >> i) & 1
}
ans = max(ans, cnt)
}
return
}
# Accepted solution for LeetCode #2275: Largest Combination With Bitwise AND Greater Than Zero
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
ans = 0
for i in range(max(candidates).bit_length()):
ans = max(ans, sum(x >> i & 1 for x in candidates))
return ans
// Accepted solution for LeetCode #2275: Largest Combination With Bitwise AND Greater Than Zero
/**
* [2275] Largest Combination With Bitwise AND Greater Than Zero
*
* The bitwise AND of an array nums is the bitwise AND of all integers in nums.
*
* For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
* Also, for nums = [7], the bitwise AND is 7.
*
* You are given an array of positive integers candidates. Compute the bitwise AND for all possible combinations of elements in the candidates array.
* Return the size of the largest combination of candidates with a bitwise AND greater than 0.
*
* Example 1:
*
* Input: candidates = [16,17,71,62,12,24,14]
* Output: 4
* Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
* The size of the combination is 4.
* It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
* Note that more than one combination may have the largest size.
* For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
*
* Example 2:
*
* Input: candidates = [8,8]
* Output: 2
* Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
* The size of the combination is 2, so we return 2.
*
*
* Constraints:
*
* 1 <= candidates.length <= 10^5
* 1 <= candidates[i] <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/
// discuss: https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn largest_combination(candidates: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2275_example_1() {
let candidates = vec![16, 17, 71, 62, 12, 24, 14];
let result = 4;
assert_eq!(Solution::largest_combination(candidates), result);
}
#[test]
#[ignore]
fn test_2275_example_2() {
let candidates = vec![8, 8];
let result = 2;
assert_eq!(Solution::largest_combination(candidates), result);
}
}
// Accepted solution for LeetCode #2275: Largest Combination With Bitwise AND Greater Than Zero
function largestCombination(candidates: number[]): number {
const mx = Math.max(...candidates);
const m = mx.toString(2).length;
let ans = 0;
for (let i = 0; i < m; ++i) {
let cnt = 0;
for (const x of candidates) {
cnt += (x >> i) & 1;
}
ans = Math.max(ans, cnt);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.