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

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

Question :

Given 2 arrays 'A' and 'B' both of size 'N'. Make 'N' pairs ( A[ i ], B[ j ] ) such that :

  1. First element of pair should be from array 'A' and second element from array 'B'. ( we cannot make pair ( A[1], A[3] ) )
  2. An element cannot occur in more than one pair. { i.e for every 2 pairs ( A[i], B[j] ) , ( A[k], B[l] ) (i != k and j != l) }
  3. Let the value of a pair be the XOR of first and second element of the pair, value of all the pairs formed must be equal.

Constraints : 0 <= A[i], B[i] <= 10^9 __________ ( 1 <= i <= N )

( Consider N even as the problem can be solved for odd N in O(N) easily )

How to solve it in less than O(N^2) Time Complexity ?

If not possible return -1

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

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

Lets try to find x,for ith bit of x let's calculate ith on and off bits in both arrays, if no. of on bits in a is equal to no.of off bits in b, then we can turn ith bit on,else we turn it off.So after finding x, we can easily find pairs by searching a[i]^x in b. Correct me if its wrong.

Sorry for English.

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

Nice Question !

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

You fix the element A[0] and then find the set of numbers S = {A[0]^B[i], 1<=i<=N}

I believe you know how it goes from here.

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

    Sorry but how is that going to help in getting near to the solution? I mean even if it would that is for sure the first step anyone would try, but what to do next is I suppose what most of us want to know.

    Can u kindly explain further steps in a bit more detail?

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

As VoidHead said, we can try to find x, using divide and conquer method. I cannot prove the correctness of the algorithm, but my solution works correctly on random tests.

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

Auto comment: topic has been updated by mtewathia99 (previous revision, new revision, compare).

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

What's the O(n) solution for odd n?