Problem : — Given an array ,find the size of the smallest subset with maximum Bitwise OR .
Constraints : — 1<=N<=100000 ; 1<=A[I]<=1000000000
I found an article which solves this problem : https://www.geeksforgeeks.org/size-of-the-smallest-subset-with-maximum-bitwise-or/
But this solution[greedy] fails on the test-case : {56,33,7}
So is there any real solution for this problem ?
Oh sorry, I dont see that you have a better solution ;-;
hi, your approach is correct but, in the loop from 64 to 0 -> if we are at a position 'p', then we have to consider every integer in the array who has 'p'th position set. We cant just consider the one which comes first in the sorted vector. As it might have a lower bit as 0 but in other int (which is lower in the sorted vector) that position might be set. for eq-> 7 91 29 58 45 51 74 70 **->This is regarding the greedy approach above.
The problem is equivalent to set cover, with univerise size log(max), and number of subsets simply N. So unless P = NP, your solution will be either exponential in N (just no...), or exponential in log(max), which could be more promising but seems difficult.
It's nothing new that GFG spreads around wrong solutions.
Is it NP hard or can be solved in Polinomial Time? Because same question asked in Google online Test 2 days before in hackerearth...
We can solve it using bitwise convolution
This is well tested code(mostly)
Pastebin link
time complexity is n logn^2, we can optimise it using binary lifting to n logn log logn
Edit : here n = 2^20
Can u explain what is inverse and transform?
see this
The above solution is not even passing sample case given in the above post.
3
56 33 7
Answer for this should be 2.
But your brute force and actual answer both giving 3. Your tested your approach with wrong brute force.
what ??? It is giving correct answer as 2
I guess you forget to comment
cin >> x;
is this approach is correct?
When you sort the array, Are you not loosing the order of elements?
Bro, we need subset not subarray, so order doesn't matter :)
A similar problem. XORSUB. just consider K = 0. and one possible solution for this is using Gaussian Elimination for XOR Maximization this will also satisfy the above constraints
Can we do this greedily by first storing the logical or of all numbers and then sorting the elements in decreasing order according to their popcount (in order decreasing number of set bits). and then if
a[i]>maxOr -> i++
else we take or with it and push it into a set, after our number has become equal to the maxOR, we stop and return the size of set. Is this fine?