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

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

How to solve this problem in time O(n log^k Cn) for some constant k? (or maybe it is impossible?)

Consider an array of size n with natural numbers not greater than C. I want to find three different elements in it (possibly their numbers will be equal) with maximal xor among all such triples.

For two elements the problem can be easily solved in time O(n log C), so I was thinking about this version. I haven't seen it before, neither on a contest nor on a training.

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

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

fourth link in Google

You can find maximum xor of K numbers in O(NlogX) (where X is maximum number in array) using Gaussian elemination (as mentioned on Quora). The unsolved part of problem for me is to find these K numbers in array.

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

    The Quora answer is just wrong. 3Sum is widely known to be O(n2), I have no idea what they're talking about. The linked article also solves the problem in . Even if 3sum were , the XOR maximization algorithm given only works if you can use the whole array.

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

If C is small you can use the "xor transform" to find the smallest/greatest value in O(C * logC). Binary search to find the first value in any triple looks ok to find the triple.

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

The problem is known as 3XOR in the literature, by the way.

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

    As far as I know, 3XOR is to find a, b, c such that a xor b = c.

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

      "a ^ b = c" is the same as "a ^ b ^ c = 0", which is the same as "(a ^ X) ^ (b ^ X) ^ (c ^ X) = X". That is, by xoring everything with X, you can ask the same 3XOR oracle, whether there is a triple that xors to a given number X.

      Now, thanks to bitwise independency of XOR, you can find the maximal xor bit by bit:

      1. Consider only the most significant bits (MSB). Is there a triple so that it xors to 1? If yes, then the MSB of the maximal xor has to be 1. Otherwise it is 0.
      2. Now add next bits. Is there a triple that xors to 11 (or 01, depending on previous answer)?

      And so on.