Count Number of Maximum Bitwise-OR Subsets

Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with that maximum bitwise OR. Two subsets are different if they pick different indices, even when the values match. The bitwise OR of an array a is a[0] OR a[1] OR ... OR a[a.length - 1].

30:00

Count Number of Maximum Bitwise-OR Subsets
medium
Free written solution
Topics
Companies

Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with that maximum bitwise OR. Two subsets are different if they pick different indices, even when the values match. The bitwise OR of an array a is a[0] OR a[1] OR ... OR a[a.length - 1].

Example 1:
Input: {"nums":[3,1]}
Output: 2
Constraints:
  • 1≤nums.length≤161 \leq \text{nums.length} \leq 16

  • 1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,1]}

OR can only switch bits on, so the largest OR any subset reaches is the OR of all 2 numbers. First compute that target.

Folding every number into target

[0]
3
[1]
1

Bits (OR only turns bits on)

[0] 3
1
1
[1] 1
0
1
target 0
0
0
Variables
VariableValue
n2
target0
DepthFunction Call
Stack empty
0/11