-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_Intersection_Array.cpp
69 lines (60 loc) · 1.26 KB
/
11_Intersection_Array.cpp
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
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
vector<int> intesection(vector<int> arr, vector<int> arr2)
{
vector<int> ans;
for (int i = 0; i < arr.size(); i++)
{
int element = arr[i];
for (int j = 0; j < arr2.size(); j++)
{
if (element == arr2[j])
{
ans.push_back(element);
arr2[j] = -2;
break;
}
}
}
return ans;
}
vector<int> printVector(vector<int> v){
for (int i = 0; i < v.size(); i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main()
{
// vector<int> v={1,2,3,4,5};
// // printVector(v);
// vector<int> v1={1,2,3,4,5,6};
int n;
cout<<"enter the size of vector 1:"<<endl;
cin>>n;
int m;
cout<<"enter the size of vector 2 :"<<endl;
cin>>m;
vector<int>v;
cout<<"enter vector1 elements:"<<endl;
for (int i = 0; i < n; i++)
{
int x;
cin>>x;
v.push_back(x);
}
vector<int>v1;
cout<<"enter vector2 elements:"<<endl;
for (int i = 0; i < m; i++)
{
int x;
cin>>x;
v1.push_back(x);
}
vector<int> ans= intesection(v, v1);
printVector(ans);
return 0;
}