1
$\begingroup$

Given a list of list of integers how to find pair of lists which have overlapping i.e. 1 or more elements are common.

Is it possible to solve without having cartesian product of initial list with itself

$\endgroup$
  • $\begingroup$ I can imagine some complex looping but not sure it would beat creating HashSets $\endgroup$ – paparazzo Feb 9 '17 at 12:20
2
$\begingroup$

Build a hash of element -> list of lists that contain that element. From that it's easy to find all pairs of lists that share an element. Of course listing all pairs is still $O(n^2)$, since there might be quadratically many. But finding just $k$ pairs is $O(k+m)$, where $m$ is the total number of elements in all sets.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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