TheAlgorithms-C-Plus-Plus/Operations on Datastructures/Intersection_of_2_arrays.cpp

32 lines
496 B
C++
Raw Normal View History

2017-12-24 01:30:49 +08:00
#include <iostream>
int main()
{
2019-08-21 10:10:08 +08:00
int i, j, m, n;
cout << "Enter size of array 1:";
2017-12-24 01:30:49 +08:00
cin >> m;
2019-08-21 10:10:08 +08:00
cout << "Enter size of array 2:";
2017-12-24 01:30:49 +08:00
cin >> n;
int a[m];
int b[n];
2019-08-21 10:10:08 +08:00
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))
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
2017-12-24 01:30:49 +08:00
else
{
2019-08-21 10:10:08 +08:00
cout << a[i++] << " ";
2017-12-24 01:30:49 +08:00
j++;
}
}
return 0;
}