-
Notifications
You must be signed in to change notification settings - Fork 1
/
ques6.cpp
52 lines (52 loc) · 1.1 KB
/
ques6.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
#include<iostream>
#include <set>
using namespace std;
void makeset(int arr1[],int arr2[],int n1,int n2){
set<int> s;
int j,i;
for(i=0;i<n1;i++){
s.insert(arr1[i]);
}
for(j=0;j<n2;j++){
s.insert(arr2[j]);
}
set<int>::iterator itr;
for (itr = s.begin();itr != s.end() ; itr++)
{
cout << *itr<<" ";
}
cout<<endl;
}
void intersection(int arr1[],int arr2[],int n1,int n2){
int i=0,j=0;
while(i<n1 && j<n2){
if(arr1[i]<arr2[j])
i++;
else if(arr2[j]<arr1[i])
j++;
else if(arr1[i] == arr2[j]){
cout<<arr1[i]<<" ";
i++;
j++;
}
}
}
int main(){
cout<<"enter size of first array"<<endl;
int n1,n2;
cin>>n1;
cout<<"enter size of second array"<<endl;
cin>>n2;
int arr1[n1],arr2[n2];
cout<<"enter first array"<<endl;
for(int i=0;i<n1;i++){
cin>>arr1[i];
}
cout<<"enter second array"<<endl;
for(int i=0;i<n2;i++){
cin>>arr2[i];
}
makeset(arr1,arr2,n1,n2);
intersection(arr1,arr2,n1,n2);
return 0;
}