If you have N sorted arrays, where the possible elements are integers from 0 ~ N-1, and the elements in one array are different, how can you check if there are at least two arrays, for example, at least two elements?
For example, if I have the following arrays, where N = 5:
A[0] = {0}, A[1] = {1, 3}, A[2] = {2}, A[3] = {1, 4}, A[4] = {1, 3}
then A [1] and A [4] both have 1 and 3 in common, and therefore the answer is correct.
In another example, where N is again 5:
A[0] = {0, 4}, A[1] = {1, 3}, A[2] = {1, 2, 4}, A[3] = {0, 3}, A[4] = {1}
No two arrays A [i], A [j] have at least two elements, so the answer is false.
This was part of an interview question that I could only solve O (n ^ 3) times by repeating each combination of arrays (A [i], A [j]) and at each iteration I scan from 0 to N-1 to check the presence of two common elements.
The interviewer indicated that there is a faster solution, and there is a hint that uses sorting arrays, but I could not find a better solution, even if I thought about this problem in the last 24 hours.
What would be a faster algorithm than O (N ^ 3) to solve this problem? Thanks.