fork download
  1. #include<stdio.h>
  2.  
  3. void findIntersection(int arr1[], int m, int arr2[], int n)
  4. {
  5. // An array to store the intersection elements
  6. int intersection_array[50] = {0};
  7. int counter = 0;
  8.  
  9. int i = 0, j = 0;
  10.  
  11. // Loop through the elements
  12. while (i < m && j < n)
  13. {
  14. if (arr1[i] > arr2[j])
  15. {
  16. // Increase iterator for array 2
  17. j++;
  18. }
  19. else if (arr2[j] > arr1[i])
  20. {
  21. // Increase iterator for array 1
  22. i++;
  23. }
  24. else
  25. {
  26. intersection_array[counter] = arr1[i];
  27. counter++;
  28.  
  29. // Increment counter for both arrays
  30. i++;
  31. j++;
  32. }
  33. }
  34.  
  35. for(i = 0; i<counter; i++)
  36. printf("%d ",intersection_array[i]);
  37. }
  38.  
  39. int main(void)
  40. {
  41. int arr1[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
  42. int arr2[] = {3, 6, 9, 12, 15, 18, 21};
  43.  
  44. findIntersection(arr1, 10, arr2, 7);
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
6 12 18