-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOf2Arrays.cpp
More file actions
62 lines (55 loc) · 1.28 KB
/
Copy pathIntersectionOf2Arrays.cpp
File metadata and controls
62 lines (55 loc) · 1.28 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
61
62
#include<iostream>
#include<vector>
using namespace std;
void Sort(int arr[], int size)
{
for (int i = 0; i < size-1; i++)
{
int minIndex = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
int main()
{
int a[]={4,9,5};
int n=3;
int b[]={9,4,9,8,4};
int m=5;
Sort(a,n);
Sort(b,m);
vector<int> ans(1,INT_MIN);
int cnt=0;
for (int i = 0; i < n; i++)
{
// int cnt1 = cnt;
for (int j = cnt; j<m; j++)
{
if (a[i]>b[j])
{
// cnt1++;
continue;
}
else if (a[i]<b[j])
break;
if (a[i]==b[j] && a[i]!=ans.back())
{
ans.push_back(a[i]);
// cout<<a[i]<<" ";
cnt++;
break;
}
}
}
ans.erase(ans.begin());
//Prints the elements present in the vector which contains the Intersection of the two arrays
for (int i = 0; i < ans.size(); i++)
cout<<ans[i]<<" ";
return 0;
}