Program to find common element between three sorted array in C with complexity o(n)
In this program find common element between the three sorted array using single while loop .In this program complexity is o(n) .
a={1,2,3} b={2,3,4} c={2,5,6}
In this three sorted array common element is 2.
Program to find common element between three sorted array in C code
#include<stdio.h>
void main()
{
int a[3]={1,2,3};
int b[3]={2,4,5};
int c[3]={2,6,7};
int temp;
int i,j,k=0;
int n;
n=3;
while(i<n&&j<n&&k<n)
{
if(a[i]==b[j]&&a[i]==c[k])
{
printf("%d",a[i]);
i++;
j++;
k++;
}
else
{
if(a[i]>b[j]&&a[i]==c[k])
j++;
if(b[j]>a[i]&&b[j]==c[k])
i++;
if(a[i]>c[k]&&a[i]==b[j])
k++;
if(a[i]>b[j]&&a[i]>c[k])
{
j++;
k++;
}
if(b[j]>a[i]&&b[j]>c[k])
{
i++;
k++;
}
if(c[k]>b[j]&&c[k]>a[i])
{
i++;
j++;
}
}
}
}
0 Comments