2
$\begingroup$

I read this link and I have similar question.

Suppose given two Arrays $A$ that is sorted array with length $n$ and $B$ unsorted array with length $n$. We want to find union of two arrays (i.e. we try to compute $A\cup B$) with comparison computation model. Can we claim that the lower bound of this problem is $\Omega(n\log n)$?

asked May 22, 2022 at 9:28
$\endgroup$
9
  • $\begingroup$ I suppose you want the resulting array to be sorted? $\endgroup$ Commented May 22, 2022 at 9:31
  • $\begingroup$ No, I want just compute $A\cup B$. $\endgroup$ Commented May 22, 2022 at 9:31
  • $\begingroup$ So you want an array of elements that appear in $A$ or in $B,ドル without duplicates? $\endgroup$ Commented May 22, 2022 at 9:33
  • $\begingroup$ According to union definition, yes. $\endgroup$ Commented May 22, 2022 at 9:34
  • 3
    $\begingroup$ Since $A$ and $B$ are not sets but arrays, it is not obvious, for example $A$ or $B$ could already contain duplicates. Mathematical notations and their use in computer science are not always clears, that's why you have to be precise in what you ask from the start. $\endgroup$ Commented May 22, 2022 at 9:44

2 Answers 2

-1
$\begingroup$

I think this is possible in $\mathcal{O}(n)$ average case, using hashtables.

You just need to create a hashtable of your elements, and before adding a new one to the result, you can check in $\mathcal{O}(1)$ average if it is already present, and add it to the hashtable if not.

I suppose here that a comparison between two elements is done in $\mathcal{O}(1)$ time.

answered May 22, 2022 at 9:37
$\endgroup$
3
  • $\begingroup$ I try to find a lower bound for the worst case. Thank you $\endgroup$ Commented May 22, 2022 at 9:38
  • 2
    $\begingroup$ Please edit your question to include ALL the details of your request. $\endgroup$ Commented May 22, 2022 at 9:46
  • 1
    $\begingroup$ (I take computed address/index to be outside comparison computation model.) $\endgroup$ Commented May 23, 2022 at 5:13
-1
$\begingroup$

But I think this is possible in $O(n(log(n)))$. Since, you can set C=A (suppose that we have no duplicate in A) and then for addition of each member from B to C it is sufficient to search it in sorted array A and if it was not in A then add the member to C.

answered May 23, 2022 at 3:56
$\endgroup$
4
  • $\begingroup$ So if every element of B is found in A: How many comparisons were needed? $\endgroup$ Commented May 23, 2022 at 4:04
  • $\begingroup$ For each element of B we have at most $log(n)$ comparisons to know that the member is not in A. Then we have $O(n(log(n)))$. $\endgroup$ Commented May 23, 2022 at 4:30
  • $\begingroup$ Now what is the But in Can we claim [this problem is in] Ω(nlogn)? and I think [finding the union of two arrays of length n] is possible in O(n(log(n)))? $\endgroup$ Commented May 23, 2022 at 4:34
  • $\begingroup$ $\Omega(n(log(n))$ means that execution of the algorithm is more than some constant multiple of $n(log(n)$ but I said that execution of the algorithm is less than some constant multiple of $n(log(n)$ and this means $O(n(log(n))$ complexity. $\endgroup$ Commented May 23, 2022 at 5:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.