Find all common numbers in given three sorted arrays.

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

Input: Three sorted arrays.

Output: All the common elements.

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 array gets over

Complete Code:


Output:

Common Elements are : 3 5 8

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: