acc1's blog

By acc1, 14 years ago, In English
Given Two arrays of n numbers. Find Whether they are disjoint or not.

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

Space : O(n)

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
If you mean expected O(n) then this is just a hash-table problem.
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
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...
  • 14 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it
    Ooops I missed to mention, O(n) memory is only there .. :P
    • 14 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it
      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.
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        I think that input file holds more memory than program can use.
14 years ago, # |
  Vote: I like it +3 Vote: I do not like it
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) .
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
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.
14 years ago, # |
  Vote: I like it +3 Vote: I do not like it
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.