-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.cpp
41 lines (40 loc) · 833 Bytes
/
6.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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(){
//program to find union and intersection of two arrays.
int n, m;
cin>>n>>m;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int arr2[m];
for(int i=0;i<m;i++)cin>>arr2[i];
int s = n + m;
int a[s], ans=0;
for(int i=0;i<n;i++){
a[i] = arr[i];
}
for(int i=0;i<m;i++){
a[i+n] = arr2[i];
}
sort(a, a+s);
for(int i=0;i<s;i++){
while(i+1<n+m && a[i]==a[i+1]){
i++;
}
ans++;
}
cout<<"Union is "<<ans<<endl;
ans = 0;
set<int> hs;
for(int i=0;i<n;i++){
hs.insert(arr[i]);
}
for(int i=0;i<m;i++){
if(hs.find(arr2[i])!=hs.end())ans++;
}
cout<<"Intersection is "<<ans<<endl;
return 0;
}