Блог пользователя LovesProgramming

Автор LovesProgramming, история, 6 лет назад, По-английски

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 ?

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится
Here is my greedy approach
Example
  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится
    Here is my another approach

    Oh sorry, I dont see that you have a better solution ;-;

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 5  
    Проголосовать: нравится +3 Проголосовать: не нравится

    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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

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.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

is this approach is correct?


1.First get total sum from the array using bit wise operator 2.After that sort the array 3.initialise a count = 0 variable and temp = arr[0] 4.loop through the sorted array(1..n): 1.temp = temp|array[i] 2.if(temp==sum): 1.print(count) 2.break 3.else operate the next element to temp 5.end
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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