#include<stdio.h>
void findIntersection(int arr1[], int m, int arr2[], int n)
{
// An array to store the intersection elements
int intersection_array[50] = {0};
int counter = 0;
int i = 0, j = 0;
// Loop through the elements
while (i < m && j < n)
{
if (arr1[i] > arr2[j])
{
// Increase iterator for array 2
j++;
}
else if (arr2[j] > arr1[i])
{
// Increase iterator for array 1
i++;
}
else
{
intersection_array[counter] = arr1[i];
counter++;
// Increment counter for both arrays
i++;
j++;
}
}
for(i = 0; i<counter; i++)
printf("%d ",intersection_array
[i
]); }
int main(void)
{
int arr1[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int arr2[] = {3, 6, 9, 12, 15, 18, 21};
findIntersection(arr1, 10, arr2, 7);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCnZvaWQgZmluZEludGVyc2VjdGlvbihpbnQgYXJyMVtdLCBpbnQgbSwgaW50IGFycjJbXSwgaW50IG4pCnsKICAgIC8vIEFuIGFycmF5IHRvIHN0b3JlIHRoZSBpbnRlcnNlY3Rpb24gZWxlbWVudHMKICAgIGludCBpbnRlcnNlY3Rpb25fYXJyYXlbNTBdID0gezB9OwogICAgaW50IGNvdW50ZXIgPSAwOwogICAgCiAgICBpbnQgaSA9IDAsIGogPSAwOwogICAgCiAgICAvLyBMb29wIHRocm91Z2ggdGhlIGVsZW1lbnRzCiAgICB3aGlsZSAoaSA8IG0gJiYgaiA8IG4pCiAgICB7CiAgICAgICAgaWYgKGFycjFbaV0gPiBhcnIyW2pdKQogICAgICAgIHsKICAgICAgICAgICAgLy8gSW5jcmVhc2UgaXRlcmF0b3IgZm9yIGFycmF5IDIKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmIChhcnIyW2pdID4gYXJyMVtpXSkKICAgICAgICB7CiAgICAgICAgICAgIC8vIEluY3JlYXNlIGl0ZXJhdG9yIGZvciBhcnJheSAxCiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgaW50ZXJzZWN0aW9uX2FycmF5W2NvdW50ZXJdID0gYXJyMVtpXTsKICAgICAgICAgICAgY291bnRlcisrOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gSW5jcmVtZW50IGNvdW50ZXIgZm9yIGJvdGggYXJyYXlzCiAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgIH0KICAgIAogICAgZm9yKGkgPSAwOyBpPGNvdW50ZXI7IGkrKykKICAgICAgICBwcmludGYoIiVkICIsaW50ZXJzZWN0aW9uX2FycmF5W2ldKTsKfQoKaW50IG1haW4odm9pZCkKewoJaW50IGFycjFbXSA9IHsyLCA0LCA2LCA4LCAxMCwgMTIsIDE0LCAxNiwgMTgsIDIwfTsKCWludCBhcnIyW10gPSB7MywgNiwgOSwgMTIsIDE1LCAxOCwgMjF9OwoJCglmaW5kSW50ZXJzZWN0aW9uKGFycjEsIDEwLCBhcnIyLCA3KTsKCQoJcmV0dXJuIDA7Cn0=