Count Number of Maximum Bitwise-OR Subsets LeetCode solution in C++
1class Solution {2public:3int countMaxOrSubsets(vector<int> &nums) {4int i, j, max_possible_or = 0, n = nums.size(), ans = 0;56// maximum possible or=or of all number in array7for (i = 0; i < n; i++) {8max_possible_or = nums[i] | max_possible_or;9}1011// checking all subset1213for (i = 1; i < (1 << n); i++) {14int p = 0;15for (j = 0; j < n; j++) {16if (i & (1 << j)) {17p = p | nums[j];18}19}20// if xor of given subset is equal to maximum possible or2122if (p == max_possible_or) {23ans++;24}25}2627return ans;28}29};