O(N)
solution (assuming the two sets are sorted):
- merge two sorted sets with information like the element belongs to which set
- traverse through the merge list , if you find two equal elements from two sets than not disjoint else if are able to reach till end than disjoint
e.g.
a= 1, 4, 6
b= 2, 4, 7
Now merged set=
elements: 1 2 4 4 6 7
set no(a/b): 1 2 1 2 1 2
Now we can clearly see that two fours are consecutive and both are from two different set, hence not disjoint.
EDIT:
If your need is to find the sets are disjoint or not than simple merge will give you that. As soon as you find both elements in the sets are equal than just return saying not disjoint else if are able to reach till end of both than disjoint.
Question related to container. Below is that:
class Element{
int i;
int setInfo
}
Now declare array as: Element[] e=new Element[X];
Hope i am clear.