Intersection Of Sorted Arrays
https://www.interviewbit.com/problems/intersection-of-sorted-arrays/
vector<int> Solution::intersect(const vector<int> &A, const vector<int> &B) {
vector<int> res;
int i=0;
int j=0;
while (i < A.size() && j < B.size())
{
if (A[i] < B[j])
{
++i;
}
else if (B[j] < A[i])
{
++j;
}
else // both are equal so insert in the result array.
{
res.push_back(A[i]);
++i;
++j;
}
}
return res;
}
Comments
Post a Comment