Ticker

6/recent/ticker-posts

Program to find common element between three sorted array in C

 

Program to find common element between three sorted array  in C 

In this program find common element between the three sorted array using three for loop(i,j,k).In this program complexity is o(n^3) which is greater that can be optimized(from O(n^3) to o(n)).

a={1,2,3}       b={2,3,4}         c={2,5,6}

In this three sorted array common element is 2.




#include<stdio.h>
void main()
{
    int a[3]={1,2,3};
    int b[3]={2,3,4};
    int c[3]={2,5,6};
    int n=3;
    int i,j,k=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[i]==b[j])
            {
                for(k=0;k<n;k++)
                {
                    if(a[i]==c[k])
                    {
                        printf("common element between three sorted array %d",a[i]);
                        break;
                    }
               
                }

                break;
   
            }
           
        }
    }
}

Output
common element between three sorted array 2

Post a Comment

0 Comments