Count Number of Maximum Bitwise-OR Subsets LeetCode solution in C++

1
class Solution {
2
public:
3
int countMaxOrSubsets(vector<int> &nums) {
4
int i, j, max_possible_or = 0, n = nums.size(), ans = 0;
5

6
// maximum possible or=or of all number in array
7
for (i = 0; i < n; i++) {
8
max_possible_or = nums[i] | max_possible_or;
9
}
10

11
// checking all subset
12

13
for (i = 1; i < (1 << n); i++) {
14
int p = 0;
15
for (j = 0; j < n; j++) {
16
if (i & (1 << j)) {
17
p = p | nums[j];
18
}
19
}
20
// if xor of given subset is equal to maximum possible or
21

22
if (p == max_possible_or) {
23
ans++;
24
}
25
}
26

27
return ans;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0