[LeetCode] Count Number of Maximum Bitwise-OR Subsets

2044. 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 the maximum bitwise OR.

An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.

The bitwise OR of an array a is equal to a[0] OR a[1] OR … OR a[a.length - 1] (0-indexed).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int countMaxOrSubsets(vector<int>& nums) {
int ma = 0, n = nums.size(), res = 0;
for(auto& n : nums) ma |= n;
for(int i = 1; i < (1<<n); i++) {
int now = 0;
for(int j = 0; j < n; j++) {
if(i & (1<<j)) now |= nums[j];
}
res += ma == now;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/11/PS/LeetCode/count-number-of-maximum-bitwise-or-subsets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.