Ctrl K

Widest Group With a Shared Bit

Medium
You get an array of positive integers numbers. Pick a group of its elements (any of them, not necessarily adjacent; repeated values count as separate elements) so that the bitwise AND of every number in the group is greater than zero. Return the largest possible size of such a group. The bitwise AND of a one-number group is that number itself, so the answer is always at least 1.
Example 1
numbers=[5, 6, 12, 3] → 3

In binary: 101, 110, 1100, 11. The bit worth 4 is set in 5, 6 and 12, and 5 & 6 & 12 = 4 > 0. All four together fail: 5 & 6 & 12 & 3 = 0.

Example 2
numbers=[9, 9, 9] → 3

Repeats are separate elements; 9 & 9 & 9 = 9, so all three fit.

Example 3
numbers=[1, 2, 4] → 1

No two of them share a set bit, so a group can hold only one number.

Constraints
  • 1 ≤ len(numbers) ≤ 10⁵
  • 1 ≤ numbers[i] < 2³¹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords From Working Keys EasyMatching Sock Pairs EasyPairs With the Same Letter Set EasyMore Than a Third of the Votes MediumMost Common Values Medium
loading Python…
Input
numbers=[5, 6, 12, 3]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.