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
|
#include<iostream>
using namespace std;
int printUnion(int B[], int c[], int m, int n)
{
int i = 0, j = 0;
while(i < m && j < n)
{
if(B[i] < c[j])
cout<< B[i++];
else if(c[j] < B[i])
cout<<c[j++];
else
{
cout<<c[j++];
i++;
}
}
/* Print remaining elements of the larger array */
while(i < m)
cout<< B[i++] <<endl;
while(j < n)
cout<< c[j++]<<endl;
return 0;
}
int printIntersection(int A[], int d[], int m1, int n1){
int i = 0, j = 0;
while(i < m1 && j < n1)
{
if(A[i] < d[j])
i++;
else if(d[j] < A[i])
j++;
else /* if arr1[i] == arr2[j] */
{
cout<< d[j++] <<endl;
i++;
}
}
return 0;
}
/* Driver program to test above function */
int main()
{
int A[]={1,2,3,4,5,6,7,8,9,10};
int B[]={2,4,6,8,10};
int c[]={1,3,5,7,9};
int d[]={1,2,3,5,7};
int m = sizeof(B)/sizeof(B[0]);
int n = sizeof(c)/sizeof(c[0]);
printUnion(B, c, m, n);
int m1= sizeof(A)/sizeof(A[0]);
int n1= sizeof(d)/sizeof(d[0]);
printIntersection(A,d,m1,n1);
//getchar();
return 0;
}
|