Maximum Group Size with Shared Bit Position
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Tests ability to use bit manipulation and counting to find the largest group of numbers sharing at least one common bit.
You are given an array of positive integers. Your task is to determine the maximum number of integers you can select such that their bitwise AND result is positive (greater than zero).
A bitwise AND of multiple numbers is positive when at least one bit position contains a 1 in all selected numbers. In other words, the selected numbers must share at least one common bit set to 1 at the same position.
Write a function that returns the size of the largest such selection.
Constraints
1 <= len(nums) <= 100,0001 <= nums[i] <= 1,000,000,000- All integers are positive
Notes
- You need only return the count, not the actual subset
- The bitwise AND of a single number is the number itself (always positive for positive integers)
- Consider all bit positions from 0 to 30 (since values are less than 2^30)
Examples
Example 1
Input: max_and_group_size([1, 2, 3, 4])
Output: 2
Example 2
Input: max_and_group_size([16, 17, 18, 19])
Output: 4
Hints
Hint 1
Think about what it means for a bitwise AND to be positive - which bit positions matter?
Hint 2
For each bit position, count how many numbers have that bit set to 1.
Hint 3
The answer is related to the bit position that appears most frequently across all numbers.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is recognizing that a bitwise AND result is positive exactly when some bit position remains set across all selected numbers. Instead of exploring all possible subsets, we can reframe this as a counting problem.
For any specific bit position k, we can select all numbers that have bit k set to 1. Since they all share this bit, their combined AND will have bit k set, guaranteeing a positive result. The question becomes: which bit position appears in the most numbers?
The algorithm:
- For each bit position from 0 to 30, count how many numbers have that bit set
- Return the maximum count across all positions
Correctness
This approach is correct because:
Lower bound (achievable): For the bit position with maximum count, we can select all numbers having that bit set. Their AND will be positive since that bit remains set in the result.
Upper bound (optimal): Consider any valid selection with positive AND. The result must have at least one bit set, say at position k. This means every number in the selection has bit k set. Therefore, the selection size cannot exceed the count of numbers with bit k set, which is at most our maximum count.
Since both bounds meet, the maximum bit count is exactly the answer.
Complexity
Time Complexity: O(n × 31) = O(n), where n is the length of the input array. We examine 31 bit positions for each number. Since 31 is a constant and values are bounded by 10^9 < 2^30, this is effectively linear.
Space Complexity: O(1). We use a fixed-size array of 31 counters regardless of input size.
Implementation
from typing import List
def max_and_group_size(nums: List[int]) -> int:
bit_counts = [0] * 31
for num in nums:
for pos in range(31):
if num & (1 << pos):
bit_counts[pos] += 1
return max(bit_counts)
For the input [1, 2, 3, 4] with binary representations 001, 010, 011, 100:
- Bit 0: appears in 1 and 3 → count = 2
- Bit 1: appears in 2 and 3 → count = 2
- Bit 2: appears in 4 → count = 1
Maximum count is 2.
For [16, 17, 18, 19], all four numbers share bit position 4 (value 16 in binary is 10000), so the answer is 4.