Practicing for software developer interviews and got stuck on an algorithm question.
Given two sets of unsorted integers with array of length m and other of
Fairly obvious approach - sort the array of length m
- O(m log m)
.
For every element in the array of length n
, use binary search to check if it exists in the array of length m
- O(log m)
per element = O(n log m)
. Since m<n
, this adds up to O(n log m)
.
Looks like Cheruvian beat me to it, but you can use a hash table to get O(n+m)
in average case:
*Insert all elements of m
into the table, taking (probably) constant time for each, assuming there aren't a lot with the same hash. This step is O(m)
*For each element of n
, check to see if it is in the table. If it is, return false. Otherwise, move on to the next. This takes O(n)
.
*If none are in the table, return true.
As I said before, this works because a hash table gives constant lookup time in average case. In the rare event that many unique elements in m
have the same hash, it will take slightly longer. However, most people don't need to care about hypothetical worst cases. For example, quick sort is used more than merge sort because it gives better average performance, despite the O(n^2)
upper bound.
Using a datastructure that has O(1) lookup/insertion you can easily insert all elements of first set.
Then foreach element in second set, if it exists not disjoint, otherwise it is disjoint
Pseudocode
function isDisjoint(list1, list2)
HashMap = new HashMap();
foreach( x in list1)
HashMap.put(x, true);
foreach(y in list2)
if(HashMap.hasKey(y))
return false;
return true;
This will give you an O(n + m) solution
Here's a link to a post that I think answers your question.
3) Sort smaller O((m + n)logm)
Disadvantage: Modifies the input