Count Number of Maximum Bitwise-OR Subsets

Problem

BACKTRACKING: OPTIMAL APPROACH:

TC :

O(2n)O(2^n)O(2n)

where n = 16 (given)

class Solution {
public int countMaxOrSubsets(int[] nums) {
int max = 0;// maximum bitwise or is the bitwise of the entire array (which is also a…


This content originally appeared on DEV Community and was authored by Prashant Mishra

Problem

BACKTRACKING: OPTIMAL APPROACH:

TC : O(2n)O(2^n)O(2n) where n = 16 (given)

class Solution {
    public int countMaxOrSubsets(int[] nums) {
        int max = 0;// maximum bitwise or is the bitwise of the entire array (which is also a subset)
        for(int i = 0;i<nums.length;i++){
            max = max | nums[i];
        }

        return count(max,0,nums,0);
    }
    public int count(int m, int i, int nums[],int val){
        if(i == nums.length){
            return m ==val ? 1:0;
        }
        int take = count(m,i+1,nums,val | nums[i]);
        int dontTake = count(m, i+1, nums,val);
        return take + dontTake;
    }
}

DP MEMOIZATION: NOT OPTIMAL, BUT WORKS ( just for understanding)

TC: O(n∗max)O(n*max)O(nmax) where max is max or of the array

class Solution {
    public int countMaxOrSubsets(int[] nums) {
        int max = 0;// maximum bitwise or is the bitwise of the entire array (which is also a subset)
        for(int i = 0;i<nums.length;i++){
            max = max | nums[i];
        }

        int dp[][] = new int[nums.length][max+1];
        for(int d[] : dp) Arrays.fill(d,-1);
        return count(max,0,nums,0,dp);
    }
    public int count(int m, int i, int nums[],int val,int dp[][]){
        if(i == nums.length){
            return m ==val ? 1:0;
        }
        if(dp[i][val]!=-1) return dp[i][val];
        int take = count(m,i+1,nums,val | nums[i],dp);
        int dontTake = count(m, i+1, nums,val,dp);
        return dp[i][val] = take + dontTake;
    }
}


This content originally appeared on DEV Community and was authored by Prashant Mishra


Print Share Comment Cite Upload Translate Updates
APA

Prashant Mishra | Sciencx (2024-10-18T16:01:15+00:00) Count Number of Maximum Bitwise-OR Subsets. Retrieved from https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/

MLA
" » Count Number of Maximum Bitwise-OR Subsets." Prashant Mishra | Sciencx - Friday October 18, 2024, https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/
HARVARD
Prashant Mishra | Sciencx Friday October 18, 2024 » Count Number of Maximum Bitwise-OR Subsets., viewed ,<https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/>
VANCOUVER
Prashant Mishra | Sciencx - » Count Number of Maximum Bitwise-OR Subsets. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/
CHICAGO
" » Count Number of Maximum Bitwise-OR Subsets." Prashant Mishra | Sciencx - Accessed . https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/
IEEE
" » Count Number of Maximum Bitwise-OR Subsets." Prashant Mishra | Sciencx [Online]. Available: https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/. [Accessed: ]
rf:citation
» Count Number of Maximum Bitwise-OR Subsets | Prashant Mishra | Sciencx | https://www.scien.cx/2024/10/18/count-number-of-maximum-bitwise-or-subsets/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.