637. Max-OR Subsets

Take a non-empty selection of numbers from nums and combine them with the bitwise OR operator. The largest value any selection can reach is the OR of the whole array. Count the non-empty subsets (selections of positions, so equal values at different positions count separately) whose OR equals that largest value.

For example, in [3, 1] the largest OR is 3, reached by {3} and {3, 1}, so the answer is 2.

Example 1

Input:
nums = [3,1]
Output:
2
Explanation:

The largest OR is 3. The subsets {3} and {3, 1} reach it; {1} gives only 1.

Example 2

Input:
nums = [2,2,2]
Output:
7
Explanation:

Every non-empty subset ORs to 2, and there are 2^3 - 1 = 7 of them.

Example 3

Input:
nums = [1,2,4,8]
Output:
1
Explanation:

The largest OR is 15 and every number is needed to reach it, so only the full set counts.

Constraints

  • 1 ≤ nums.length ≤ 30
  • 1 ≤ nums[i] ≤ 4095

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 800 msC++ 200 msJava 400 msJavaScript 400 msTypeScript 400 ms

Expected complexity

Time
O(n * 4096)
Space
O(4096)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…