Be the first user to complete this post

  • 0
Add to List
Beginner

52. Find Common Numbers in Three Sorted Arrays

Objective: Given three sorted(ascending order) arrays of integers, find out all the common elements in them.

Examples :

Array A = {1,2,3,4,5,6,7,8,9,10};
Array B = {1,3,5,6,7,8,12};
Array C = {2,3,4,5,8,9};
Common Elements are 3,5,8
Approach:
  • Very Simple Solution.
  • Navigate all three arrays(A, B, C) simultaneously using indexes say, i,j,k.
  • if(A[i]==B[j]==C[k]) then print A[i] and do i++, j++, k++.
  • if not then compare all A[i],B[j],C[k] and which ever is smaller, increase its index.
  • Stop when any of these arrays gets over
 
Common Elements are : 3 5 8