-
Notifications
You must be signed in to change notification settings - Fork 85
/
Array Intersection.cpp
76 lines (63 loc) · 1.36 KB
/
Array Intersection.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
70
71
72
73
74
75
76
/*
Question :
You have been given two integer arrays/list(ARR1 and ARR2) of size M and N, respectively.
You need to print their intersection;
An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words,
when there is a common value that exists in both the arrays/lists.
*/
// Code :
#include <iostream>
#include <algorithm>
#include<climits>
using namespace std;
//Array Intersection Function
void intersection(int *input1, int *input2, int size1, int size2)
{
int i=0,j=0,k=0,f=0,n=0;
int b[size1];
for(i=0;i<size1;i++)
{
for(j=0;j<size2;j++)
{
if(*(input1+i)==*(input2+j))
{
b[n]=*(input2+j);
n++;
*(input1+i)=INT_MIN;
*(input2+j)=INT_MIN;
break;
}
}
}
for(i=0;i<n;i++)
{
cout<<b[i]<<" ";
}
}
// Driver Code
int main()
{
int t;
cin >> t;
while (t--)
{
int size1, size2;
cin >> size1;
int *input1 = new int[size1];
for (int i = 0; i < size1; i++)
{
cin >> input1[i];
}
cin >> size2;
int *input2 = new int[size2];
for (int i = 0; i < size2; i++)
{
cin >> input2[i];
}
intersection(input1, input2, size1, size2);
delete[] input1;
delete[] input2;
cout << endl;
}
return 0;
}