vector SortedIntersection(int *a, int an, int *b, int bn)
{
vector result;
// assuming asscending order.
int *aend = a + an;
int *bend = b + bn;
while (a != aend && b != bend)
{
int candidate = *a;
++a;
while (*b < candidate && b != bend)
{
++b;
}
if (b != bend && candidate == *b)
{
result.push_back(candidate);
++b;
}
}
return result;
}
1
Anonymous
26 Nov 2011
Should clarify what intersection means, after that it's pretty straightforward
Anonymous
3 Dec 2011
It can be solved in O(nlogn) . For each number in the 1st array, do a binary search in the second one, if found - try comparing the two subsets.