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

Автор acc1, 13 лет назад, По-английски
Given Two arrays of n numbers. Find Whether they are disjoint or not.

Each Element lie in range [0,n^100] .

Space : O(n)

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

13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
If you mean expected O(n) then this is just a hash-table problem.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Looks like a hash-related problem, but not sure about n^100!
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
You can unify these arrays to one and sort it by radix sort. Then you can find two adjanced equal items in large array, which were in different arrays. If you can find this, arrays aren't disjoint.

Sorry for bad English...
  • 13 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится
    Ooops I missed to mention, O(n) memory is only there .. :P
    • 13 лет назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится
      And this algo uses only O(n) memory because radix-sort is O(n) memory and O(k*n) time (but k is const) and check-up is O(1) memory and O(n) time.
13 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится
Hash - Table ..mmm..but it is implemented using rb tree, then how come O(n) ? , then it will get O(n*lgn) ....

Answer above question, and one more thing , can we do this using bitwise kung-fu , like Xoring (just a guess) .
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
How about using a set? Store all the numbers of the first array in a set. A set only requires O(log(n)) time for instertion and search. And it also requires O(n) memory AFAIK.
13 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится
Size of input in this problem is O(n * log(n100)) = O(nlog(n)) so it can't be solved in O(n) time. At least unless we have some special agreements about working with long numbers in O(1) time.