Skip to main content
Back to problems
#2275
Medium Algorithms

Largest combination with bitwise and greater than zero

Array Hash Table Bit Manipulation Counting
80.8% acceptance
Feb 25, 2026
1132
60
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.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn largest_combination(candidates: Vec<i32>) -> i32 {
    let mut bit_count = [0i32; 24];
    for &c in &candidates {
      for b in 0..24 {
        if c & (1 << b) != 0 {
          bit_count[b] += 1;
        }
      }
    }
    *bit_count.iter().max().unwrap()
  }
}