-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_array_intersections.cpp
More file actions
60 lines (46 loc) · 1.16 KB
/
Copy pathfind_array_intersections.cpp
File metadata and controls
60 lines (46 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Here, you need to find the intersection elements of two given arrays.
They can hold duplicates which will be matched based on their sequence of appearance.
Time complexity: O(n*m)
where n is the number of elements of vector1 and m is the number of elements of vector2.
*/
#include<bits/stdc++.h>
using namespace std;
void print_intersection_vector(vector<int> v1, vector<int> v2)
{
vector<int>::iterator i,j;
vector<int> vout;
i=v1.begin();
j=v2.begin();
while(j!=v2.end())
{
if(*i==*j)
{
vout.push_back(*i);
v1.erase(i);
v2.erase(j);
i=v1.begin();
j=v2.begin();
}
else
{
++i;
if(i==v1.end())
{
i=v1.begin();
++j;
}
}
}
cout<<"Printing the vector of intersection elements:\n";
for(i=vout.begin();i!=vout.end();i++)
cout<<*i<<"\t";
}
int main()
{
vector<int> v1{2,6,1,2};
vector<int> v2{1,2,3,4,2};
print_intersection_vector(v1,v2);
cout<<endl;
return 0;
}