2275. Largest Combination With Bitwise AND Greater Than Zero - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given an array of positive integers, your task is to choose a combination (i.e., a subset) of these integers such that the bitwise AND of all numbers in the combination is greater than 0. In other words, you want to select as many numbers as possible so that there exists at least one bit position where every selected number has a 1. The goal is to return the maximum number of integers you can choose that satisfy this property.

Example 1

  • Input: candidates = [16,17,71,62,12,24,14]

  • Output: 4

  • Explanation:
    One possible combination of maximum size is [16,62,12,24]. Notice that each of these numbers has the 4th bit (from the right, 0-indexed) set (i.e., the bit corresponding to the value 8 is 1 in each number), ensuring that the bitwise AND of the combination is at least 8, which is greater than 0.

Example 2

  • Input: candidates = [8,8]
  • Output: 2
  • Explanation:
    Both numbers are the same and are 8 (binary 1000). Their bitwise AND is 8, and the largest valid combination uses both numbers.

Example 3

  • Input: candidates = [1,2,3,4]
  • Output: 2
  • Explanation:
    One optimal choice is [1,3] (both have the least significant bit set) or even [2,4] if you consider another common bit. The maximum possible valid combination here contains 2 numbers.

Constraints:

  • 1 ≤ candidates.length ≤ 10⁵
  • 1 ≤ candidates[i] ≤ 10⁹

Hints

  1. Common Bit Observation:
    For a combination of numbers to have a bitwise AND greater than 0, they must share at least one common set bit (i.e., there exists a bit position where every number in the combination has a 1).

  2. Bit-by-Bit Analysis:
    Instead of trying all possible subsets (which is infeasible for large arrays), consider each bit position (from 0 to 31, because candidates are positive integers within 32-bit range). For each bit position, count how many numbers have that bit set.

Detailed Explanation

The key insight for this problem is that the bitwise AND of a set of numbers is greater than 0 if and only if there exists at least one bit position where every number in the set has that bit set to 1. Instead of testing every possible subset, you can solve the problem by answering the following question for each bit position:

  • How many numbers in the array have the i-th bit set?

If you have a count for a particular bit position ( i ), then you can choose all numbers that have that bit set and the bitwise AND of that combination will always be at least ( 2^i ) (because that bit will remain set in the AND result). Therefore, the maximum number of numbers you can choose such that their bitwise AND is non-zero is simply the maximum count among all bit positions.

Step-by-Step Walkthrough:

  1. Initialize an Answer Variable:
    Begin with a variable (say, ans) that will track the maximum count of numbers sharing a common bit.

  2. Iterate Over All 32 Bit Positions:
    For each bit ( i ) from 0 to 31:

    • Initialize a counter to 0.
    • For every number in the candidates array, check if the ( i )-th bit is set. This is done using the expression:
      if (number & (1 << i)) != 0:
      
      If the condition is true, increase the counter.
  3. Update Maximum Count:
    After processing each bit position, update ans with the maximum value between the current ans and the count for that bit.

  4. Return the Result:
    Once all bits have been processed, the variable ans contains the size of the largest combination where the bitwise AND is greater than 0.

Why This Works:

  • A valid combination must have at least one bit common to all its members.
  • By selecting the bit with the highest frequency in the array, you automatically ensure that every number in that subset has that bit set.
  • Thus, the maximum frequency among all bits is the answer.

Complexity Analysis:

  • Time Complexity: ( O(32 \times n) ) which simplifies to ( O(n) ) because there are always 32 iterations over ( n ) numbers.

  • Space Complexity: ( O(1) ) additional space (only a few counter variables are used).

Code Solutions

Python Implementation

Python3
Python3

. . . .

Java Implementation

Java
Java

. . . .

Common Mistakes

  • Overcomplicating the Approach:
    Some might try to use complex bitmask or subset generation methods. However, the key insight is to realize that you only need to count common bits.

  • Not Considering All Bit Positions:
    Ensure that you iterate through all 32 bit positions; missing even one may lead to a wrong result.

  • Incorrect Bit Checking:
    Forgetting to check the bit properly (e.g., using an incorrect bit shift) might cause logical errors.

Edge Cases

  • All Numbers Are Identical:
    If every number is the same, then every bit set in that number contributes to the answer. The maximum combination size is the length of the array.

  • Only One Number Contains a Specific Bit:
    Even if one bit is set in only one number, the largest valid combination might be of size 1. This is important because if no bit is common among more than one number, the answer is still at least 1 (assuming there is at least one number).

  • Sparse Bit Patterns:
    When numbers have very few bits set, the maximum count might be low, but the algorithm still correctly computes the result by considering each bit individually.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;