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

Автор pritishn, 4 года назад, По-английски

So, basically this problem can be considered as a simpler version of https://codeforces.me/blog/entry/74836 . I was wondering what can be the approach if we fix the size of the subset.

Given an array of size N , find the maximum bitwise or of elements in a subset of size K.

What can be the best(time complexity wise) possible approach for this problem apart from this ? Can anyone please help?

UPD: I had initially written Xor everywhere instead of Or. Sorry.

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

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

Clarification- Is the subset size fixed = k?

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

If K is a part of the input to the problem, then this is not a simpler version of the task "given an array of size N, find the maximum bitwise xor of any subset of elements", as you could:

  1. Take any input to the problem without any fixed $$$K$$$; say, it contains $$$N$$$ integers.
  2. Pad the array with $$$N$$$ zeroes (so that the new array has $$$N' = 2N$$$ integers).
  3. Set $$$K' = N$$$ (that is, $$$K' = \frac12 N'$$$).
  4. We get an instance of your problem, with an array of $$$N'$$$ integers, where we want to maximize the xor of elements in a subset of size $$$K'$$$.

Now, you can easily see that there exists a solution with XOR = $$$x$$$ to the new problem (array of size $$$N'$$$, we want to find a subset of size $$$K'$$$) if and only if there exists a solution with XOR = $$$x$$$ to the original problem. So, in some sense, your problem is at least as hard as the original problem.

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

    Shit, I realized I wrote XOR by mistake. Sorry for this extremely dumb mistake. I intended to write OR.

    Anyway, Thanks a lot for your insight.

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

      Oh, okay. So I believe my construction still works? And if the original problem is NP-complete (assuming the numbers can be arbitrarily large, and not bounded by something like 1e5), then your problem should be NP-complete as well.

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

        Yeah, I understood your point. For arbitrarily large numbers your points seem valid.

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

        Annoying purist here: I don't see how the maximisation problem is in NP. What you probably meant to say was NP-hard.

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

    If K is not fixed, we can just use Gaussian Elimination as described in this article, and find the maximum Xor in O(n*log(A_max)).

    P.S- Author just changed xor to or...This is one of the solution for xor.

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

      Well, apparently you're right. :D

      So it's an interesting question if we can solve the OP's problem with XOR instead of OR. It's weird because it's "a thing I should probably know about", but somehow I don't.