1
$\begingroup$

In two unsorted arrays (say $A$ and $B$) such that all items of $A$ belong to $B$ ($A \subset B$), find the smallest item in $B$ that doesn't occur in $A$ in linear time.

No hashing or linear time sorting is allowed.

I can solve this question when $A$ and $B$ are sorted.
However, the question states $A$ and $B$ are unsorted, and I cannot think of a solution that solves this in O(n) time.

$\endgroup$
2
  • 2
    $\begingroup$ Why do you think it can be done in linear time? Where did you encounter this question, and what was the source where you saw it? $\endgroup$
    – D.W.
    Commented Oct 18, 2022 at 1:57
  • $\begingroup$ Why would hashing not be allowed? Solving a problem with one hand tied behind your back is rather stupid, don't you think so? $\endgroup$
    – gnasher729
    Commented Oct 19, 2022 at 14:32

2 Answers 2

3
$\begingroup$

I'm going to assume that all elements in $A$ and $B$ are distinct (this assumption can be removed with some care), that elements possess a linear order, and that they can be compared in constant time.

Consider the following recursive algorithm: if $|A|=0$, return the minimum in $B$ (in time $O(|B|)$). Otherwise, find the median $x$ of the multiset obtained by unioning $A$ and $B$ and partition the elements of $A$ in $A_{\le x}$ and $A_{> x} $ (resp. the elements of $B$ in $B_{\le x}$ and $B_{> x} $) depending on whether they are at most $x$ or larger than $x$. This requires time $O(|A|+|B|)$.

If $|A_{\le x}|=|B_{\le x}|$ then $A \subset B$ implies $A_{\le x}=B_{\le x}$ and you can recurse on $A_{> x} $ and $B_{> x}$.

If $|A_{\le x}| < |B_{\le x}|$ then the minimum element in $B \setminus A$ must be at most $x$ and hence it is in $B_{\le x} \setminus A_{\le x}$ and you can recurse on $A_{\le x}$ and $B_{\le x}$.

Let $n = |A| + |B|$. By the choice of $x$, the input size of the recursive call will be at most $\left\lceil \frac{|A|+|B|}{2} \right\rceil = \lceil n/2 \rceil$, showing that the algorithm eventually terminates (since $n \le 2$ implies $|A|=0$).

To bound the running time we notice that it is described by the recurrence $ T(n) = T(\lceil n/2 \rceil) + O(n) $, which has solution $T(n)=O(n)$.

$\endgroup$
2
  • $\begingroup$ It would seem to be fatal if A or B has two identical elements. First, A(<= x) can have more elements than B(<= x), and your conclusions from comparing the number of elements will fail. Of course a hash table can take care of that quite easily and remove duplicates. $\endgroup$
    – gnasher729
    Commented Oct 19, 2022 at 14:41
  • $\begingroup$ If $A$ and $B$ are multisets (and $A \subset B$, counting multiplicities) the above approach works but the analysis of the time complexity breaks down. However we can easily restore it by partitioning $A$ and $B$ into $3$ sets each. The sets contain the elements smaller than, equal to, and larger than $x$. If $A$ and $B$ represent sets but somehow can contain the same element more than once (and the multiplicity of an element doesn't matter), then I don't know how to solve the problem in linear time in the worst case. $\endgroup$
    – Steven
    Commented Oct 19, 2022 at 19:14
2
$\begingroup$

Reasoning:

  • you are asked the smallest in $B$; this hints that the solution will be comparison-based.

  • with an $O(n)$ budget, no comparison-based sort is possible; the best you can do with comparisons is to heapify !

  • wonder what happens if you heapify the two arrays and compare them.

Notice that after a heapify operation, every array element is the smallest among the subtree of which it is a root. So when you compare the heaps formed from $A$ and $B$, the first discrepancy you will meet when scanning the elements will reveal the solution.

E.g.

$$A= [1,2,3,4,6],\\ B= [6,2,4,7,1,5,3]$$

heapify as

$$A=[1, 2, 3, 4, 6],\\B= [1, 2, 3, \color{green}7, 6, 5, 4].$$

$\endgroup$
2
  • 1
    $\begingroup$ I don't think this works $A = [1,3,2]$ and $B = [1,2,3,4]$ are both already heaps but the first discrepancy is at the second position. $\endgroup$
    – Steven
    Commented Oct 18, 2022 at 17:18
  • $\begingroup$ @Steven: that's right. My tests did not show that. :-( $\endgroup$
    – user16034
    Commented Oct 18, 2022 at 19:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.