fork download
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. #include <vector>
  4. #include <cmath>
  5. using namespace std ;
  6. class Solution
  7. {
  8. public:
  9. int maxOR;
  10. int Count;
  11.  
  12. void solve(const vector<int>& nums, int index, int currentOR)
  13. {
  14. if (index >= nums.size()) {
  15. if (currentOR == maxOR)
  16. Count++;
  17. return;
  18. }
  19. // take
  20. solve(nums, index + 1, currentOR | nums[index]);
  21. // leave
  22. solve(nums, index + 1, currentOR);
  23. }
  24.  
  25. int countMaxOrSubsets(vector<int>& nums)
  26. {
  27. maxOR = 0;
  28. Count = 0;
  29.  
  30. for (int num : nums)
  31. maxOR |= num;
  32.  
  33. solve(nums, 0, 0);
  34.  
  35. return Count;
  36. }
  37. };
  38.  
  39. int main() {
  40. Solution sol;
  41. vector<int> nums = {2,2,2};
  42. cout << "Count of max OR subsets: " << sol.countMaxOrSubsets(nums) << endl;
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Count of max OR subsets: 7