Added union and intersection of 2 arrays.

This commit is contained in:
Shivam Singhal
2017-06-06 12:50:27 +05:30
parent 74b6428eed
commit c1f2201e94
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
#include <iostream>
int main()
{
int i,j,m,n;
cout <<"Enter size of array 1:";
cin >> m;
cout <<"Enter size of array 2:";
cin >> n;
int a[m];
int b[n];
cout <<"Enter elements of array 1:";
for(i=0;i<m;i++)
cin >> a[i];
for(i=0;i<n;i++)
cin >> b[i];
i=0;j=0;
while((i<m)&&(j<n))
{
if(a[i]<b[j])
i++;
else if(a[i]>b[j])
j++;
else
{
cout << a[i++]<<" ";
j++;
}
}
return 0;
}

33
Union_of_2_arrays.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
int main()
{
int m,n,i=0,j=0;
cout << "Enter size of both arrays:";
cin >> m >> n;
int a[m];
int b[n];
cout << "Enter elements of array 1:";
for(i=0;i<m;i++)
cin >>a[i];
cout << "Enter elements of array 2:";
for(i=0;i<n;i++)
cin >> b[i];
i=0;j=0;
while((i<m)&&(j<n))
{
if(a[i]<b[j])
cout << a[i++] <<" ";
else if(a[i]>b[j])
cout << b[j++] <<" ";
else
{
cout << a[i++];
j++;
}
}
while(i<m)
cout<< a[i++] <<" ";
while(j<n)
cout<< b[j++]<<" ";
return 0;
}