🧠
Brute Force (Pure Recursion / Backtracking)
💡 This approach exists to build intuition by exploring every subset explicitly. It helps beginners understand the problem's exponential nature and the mechanics of subset generation.
Intuition
Generate all subsets recursively, compute their bitwise OR, track the maximum OR found, and count how many subsets achieve this maximum.
Algorithm
- Initialize max_or to 0 and count to 0.
- Use recursion to explore subsets by including or excluding each element.
- At each leaf (end of recursion), compute the OR of the chosen subset.
- Update max_or and count accordingly.
💡 The challenge is to systematically explore all subsets without missing any and correctly update the maximum OR and count as you go.
from typing import List
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
self.max_or = 0
self.count = 0
def backtrack(i: int, current_or: int):
if i == len(nums):
if current_or > self.max_or:
self.max_or = current_or
self.count = 1
elif current_or == self.max_or:
self.count += 1
return
# Include nums[i]
backtrack(i + 1, current_or | nums[i])
# Exclude nums[i]
backtrack(i + 1, current_or)
backtrack(0, 0)
return self.count
# Driver code
if __name__ == '__main__':
sol = Solution()
print(sol.countMaxOrSubsets([3,1])) # Expected output: 2
print(sol.countMaxOrSubsets([2,2,2])) # Expected output: 7
Line Notes
self.max_or = 0Initialize the maximum OR found so far to zero to track the best OR value
self.count = 0Initialize count of subsets achieving max OR to zero
def backtrack(i: int, current_or: int):Recursive helper to explore subsets starting at index i with current OR
if i == len(nums):Base case: all elements considered, time to update max_or and count
if current_or > self.max_or:Found a new maximum OR, reset count to 1
elif current_or == self.max_or:Found another subset matching max OR, increment count
backtrack(i + 1, current_or | nums[i])Include current element in subset and update OR
backtrack(i + 1, current_or)Exclude current element and keep OR unchanged
return self.countReturn the total count of max OR subsets
import java.util.*;
public class Solution {
private int maxOr = 0;
private int count = 0;
public int countMaxOrSubsets(int[] nums) {
backtrack(nums, 0, 0);
return count;
}
private void backtrack(int[] nums, int index, int currentOr) {
if (index == nums.length) {
if (currentOr > maxOr) {
maxOr = currentOr;
count = 1;
} else if (currentOr == maxOr) {
count++;
}
return;
}
// Include nums[index]
backtrack(nums, index + 1, currentOr | nums[index]);
// Exclude nums[index]
backtrack(nums, index + 1, currentOr);
}
// Driver code
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.countMaxOrSubsets(new int[]{3,1})); // Expected: 2
System.out.println(sol.countMaxOrSubsets(new int[]{2,2,2})); // Expected: 7
}
}
Line Notes
private int maxOr = 0;Store the maximum OR found so far to track best OR value
private int count = 0;Count subsets achieving max OR
if (index == nums.length)Base case: all elements processed
if (currentOr > maxOr)Update max OR and reset count to 1
else if (currentOr == maxOr)Increment count for matching max OR
backtrack(nums, index + 1, currentOr | nums[index]);Include current element in subset
backtrack(nums, index + 1, currentOr);Exclude current element
return;End recursion branch
#include <iostream>
#include <vector>
using namespace std;
class Solution {
int maxOr = 0;
int count = 0;
void backtrack(const vector<int>& nums, int index, int currentOr) {
if (index == nums.size()) {
if (currentOr > maxOr) {
maxOr = currentOr;
count = 1;
} else if (currentOr == maxOr) {
count++;
}
return;
}
// Include nums[index]
backtrack(nums, index + 1, currentOr | nums[index]);
// Exclude nums[index]
backtrack(nums, index + 1, currentOr);
}
public:
int countMaxOrSubsets(vector<int>& nums) {
backtrack(nums, 0, 0);
return count;
}
};
int main() {
Solution sol;
vector<int> nums1 = {3,1};
cout << sol.countMaxOrSubsets(nums1) << endl; // Expected: 2
vector<int> nums2 = {2,2,2};
cout << sol.countMaxOrSubsets(nums2) << endl; // Expected: 7
return 0;
}
Line Notes
int maxOr = 0;Track maximum OR value found to keep best OR
int count = 0;Count subsets with max OR
if (index == nums.size()) {Base case: all elements considered
if (currentOr > maxOr) {Found new max OR, reset count to 1
else if (currentOr == maxOr) {Increment count for equal max OR
backtrack(nums, index + 1, currentOr | nums[index]);Include current element
backtrack(nums, index + 1, currentOr);Exclude current element
return;End recursion branch
var countMaxOrSubsets = function(nums) {
let maxOr = 0;
let count = 0;
function backtrack(i, currentOr) {
if (i === nums.length) {
if (currentOr > maxOr) {
maxOr = currentOr;
count = 1;
} else if (currentOr === maxOr) {
count++;
}
return;
}
// Include nums[i]
backtrack(i + 1, currentOr | nums[i]);
// Exclude nums[i]
backtrack(i + 1, currentOr);
}
backtrack(0, 0);
return count;
};
// Driver code
console.log(countMaxOrSubsets([3,1])); // Expected: 2
console.log(countMaxOrSubsets([2,2,2])); // Expected: 7
Line Notes
let maxOr = 0;Initialize max OR to zero to track best OR value
let count = 0;Initialize count of max OR subsets
function backtrack(i, currentOr)Recursive function exploring subsets starting at index i
if (i === nums.length)Base case: all elements processed
if (currentOr > maxOr)Update max OR and reset count to 1
else if (currentOr === maxOr)Increment count for subsets matching max OR
backtrack(i + 1, currentOr | nums[i]);Include current element in subset
backtrack(i + 1, currentOr);Exclude current element
TimeO(2^n)
SpaceO(n) due to recursion stack
We explore all subsets (2^n). Each subset requires O(1) OR operations cumulatively during recursion.
💡 For n=16, 2^16 = 65,536 subsets, which is borderline feasible but slow for larger n.
Interview Verdict: Accepted for small n, but inefficient for large inputs
This approach is a baseline to understand the problem but will time out for large inputs due to exponential growth.