The Indian Engineer

Problem 2044 Count Number of Maximum Bitwise-OR Subsets

Posted on 7 mins

Array Backtracking Bit-Manipulation Enumeration

Problem Statement

Link - Problem 2044

Question

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).

Example 1:

Input: nums = [3,1]
Output: 2
Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
- [3]
- [3,1]

Example 2:

Input: nums = [2,2,2]
Output: 7
Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 23 - 1 = 7 total subsets.

Example 3:

Input: nums = [3,2,1,5]
Output: 6
Explanation: The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
- [3,5]
- [3,1,5]
- [3,2,5]
- [3,2,1,5]
- [2,5]
- [2,1,5]

Constraints:

Solution

class Solution {
public:
    int countMaxOrSubsets(vector<int>& nums) {
        int n = nums.size();
        int maxOrValue = 0;
        for (int num : nums) {
            maxOrValue |= num;
        }

        vector<vector<int>> memo(n, vector<int>(maxOrValue + 1, -1));

        return countSubsetsRecursive(nums, 0, 0, maxOrValue, memo);
    }

private:
    int countSubsetsRecursive(vector<int>& nums, int index, int currentOr, int targetOr, vector<vector<int>>& memo) {

        if (index == nums.size()) {
            return (currentOr == targetOr) ? 1 : 0;
        }

        if (memo[index][currentOr] != -1) {
            return memo[index][currentOr];
        }

        int countWithout = countSubsetsRecursive(nums, index + 1, currentOr, targetOr, memo);

        int countWith = countSubsetsRecursive(nums, index + 1, currentOr | nums[index], targetOr, memo);

        return memo[index][currentOr] = countWithout + countWith;
    }
};

Complexity Analysis

| Algorithm                   | Time Complexity | Space Complexity |
| --------------------------- | --------------- | ---------------- |
| Memoized Depth-First Search | O(n 2^n)        | O(n 2^n )        |

Explanation

Intial Thoughts

To approach this problem, we should first consider the properties of bitwise OR operations. The maximum possible bitwise OR of a subset can be obtained by including all elements in the subset. Then, we need to find the number of subsets that have this maximum bitwise OR. We should also consider the concept of subsets and how to generate them. Additionally, we should think about how to optimize the solution to avoid unnecessary computations. The problem can be broken down into smaller sub-problems, such as finding the maximum bitwise OR and counting the number of subsets with this maximum value. We can use a recursive approach or dynamic programming to solve this problem. The time complexity and space complexity of the solution should also be considered.

Intuitive Analysis

Intuitively, we can think of the problem as finding the number of ways to combine the elements in the array to get the maximum possible bitwise OR. This can be achieved by considering each element in the array and deciding whether to include it in the subset or not. We can use a tree-like structure to represent the possible subsets, where each node represents a decision to include or exclude an element. The maximum bitwise OR can be obtained by considering the elements with the most significant bits set. We can also use the concept of memoization to store the results of sub-problems and avoid redundant computations. By analyzing the problem intuitively, we can develop a solution that is both efficient and effective. The key is to find a balance between exploring all possible subsets and avoiding unnecessary computations. We should also consider the constraints of the problem, such as the size of the input array, to optimize the solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Can we enumerate all possible subsets?

The maximum bitwise-OR is the bitwise-OR of the whole array.


Similar Questions:

Title URL Difficulty
Subsets https://leetcode.com/problems/subsets Medium
Largest Combination With Bitwise AND Greater Than Zero https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero Medium
Longest Subarray With Maximum Bitwise AND https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and Medium