-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path6_UnionAndIntersectionOfArrays.cpp
135 lines (103 loc) · 2.18 KB
/
6_UnionAndIntersectionOfArrays.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Author - Swati(swati_gwc)
*/
#include <bits/stdc++.h>
using namespace std;
void findunion(int arr1[],int size1,int arr2[],int size2)
{
int i=0, j=0,k=0;
int arr3[size1+size2];
while(i<size1 && j<size2)
{
if(arr1[i]<arr2[j])
{
arr3[k]=arr1[i];
i++;
}
else if(arr1[i]>arr2[j])
{
arr3[k] = arr2[j];
j++;
}
else if(arr1[i]==arr2[j])
{
arr3[k]=arr1[i];
i++; j++;
}
k++;
}
while(i<size1){
arr3[k]=arr1[i];
k++;
i++;
}
while(j<size2){
arr3[k]=arr2[j];
k++;
j++;
}
cout<<"\nUninon of array 1 and 2: ";
for(i=0;i<k; i++)
{
cout<<arr3[i]<<" ";
}
cout<<endl;
}
void findintersection(int arr1[],int size1,int arr2[],int size2)
{
int i=0, j=0,k=-1;
int arr3[size1+size2];
while(i<size1 && j<size2)
{
if(arr1[i]<arr2[j])
{
i++;
}
else if(arr1[i]>arr2[j])
{
j++;
}
else if(arr1[i]==arr2[j])
{
arr3[++k]=arr1[i];
i++;j++;
}
}
cout<<"\nIntersection of array 1 and 2: ";
if(k==-1){
cout<<"No common elements! ";
return;
}
for(i=0;i<=k; i++)
{
cout<<arr3[i]<<" ";
}
cout<<endl;
}
int main() {
int i,size1,size2;
cout<<"Enter elements in sorted order "<<endl<<endl;
cout<<"Enter size of array 1: ";
cin>>size1;
int arr1[size1];
cout<<"Enter elements of array 1 : ";
for(i=0; i<size1; i++)
cin>>arr1[i];
cout<<"\nEnter size of array 2: ";
cin>>size2;
int arr2[size2];
cout<<"Enter elements of array 2 : ";
for(i=0; i<size2; i++)
cin>>arr2[i];
findunion(arr1,size1,arr2,size2);
findintersection(arr1,size1,arr2,size2);
return 0;
}
/*SAMPLE OUTPUT:
Enter elements in sorted order
Enter size of array 1: 4
Enter elements of array 1 : 1 3 4 5
Enter size of array 2: 6
Enter elements of array 2 : 1 4 5 6 7 8
Uninon of array 1 and 2: 1 3 4 5 6 7 8
Intersection of array 1 and 2: 1 4 5*/