-
Notifications
You must be signed in to change notification settings - Fork 0
/
Union&Intersection.cpp
100 lines (96 loc) · 2.08 KB
/
Union&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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t, a, b, c, d, x, y, z;
vector<int> v1;
vector<int> v2;
vector<int> v3;
char str;
cin >> a >> b;
cin >> str;
// initializing v1 and v3
for (int j = 0; j < a; j++)
{
cin >> x;
v1.push_back(x);
v3.push_back(x);
// cout << j << endl;
}
// initializing v2 and v3
for (int k = 0; k < b; k++)
{
cin >> y;
v2.push_back(y);
v3.push_back(y);
}
int ct = v3.size();
int count = 0;
//Union Part
// delete duplicate elements
for (int i = 0; i < ct; i++)
{
for (int j = i + 1; j < ct; j++)
{
if (v3[i] == v3[j])
{
for (int k = j; k < ct; k++)
{
v3[k] = v3[k + 1];
}
j--;
ct--;
// count matching elements number
count++;
}
}
}
// delete 0(Null) elements from vector
for (int i = 0; i < count; i++)
{
v3.pop_back();
}
// sort the vector
sort(v3.begin(), v3.end());
if (str == 'U')
{
// union results print
for (int i = 0; i < v3.size(); i++)
{
cout << v3[i] << " ";
}
}
// intersection part
if (str == 'I')
{
//find common element if a small
if (a < b)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
if (v1[i] == v2[j])
{
cout << v1[i] << " ";
}
}
}
}
//find common element if b small
if (a > b)
{
for (int i = 0; i < b; i++)
{
for (int j = 0; j < a; j++)
{
if (v2[i] == v1[j])
{
cout << v2[i] << " ";
}
}
}
}
}
return 0;
}