Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

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

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
  • Проголосовать: не нравится

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

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

  • »
    »
    4 года назад, # ^ |
    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.

»
5 лет назад, # |
  Проголосовать: нравится +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.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +15 Проголосовать: не нравится

    Is it NP hard or can be solved in Polinomial Time? Because same question asked in Google online Test 2 days before in hackerearth...

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится +4 Проголосовать: не нравится

      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

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Can u explain what is inverse and transform?

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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.

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          what ??? It is giving correct answer as 2
          I guess you forget to comment cin >> x;

»
4 года назад, # |
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
»
4 года назад, # |
  Проголосовать: нравится 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

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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?