Ticker

6/recent/ticker-posts

Program to find common b/w three sorted list with complexity O(n) in python.



Program to find common three sorted list with complexity O(n) in python.



 def fc(a,b,c):

    n=len(a)

    h=len(b)

    k=len(c)

    x=0

    y=0

    z=0

     while(x<n and y<h and z<k):

        if(a[x]==b[y] and a[x]==c[z]):

            print(a[x])

            x=x+1

            y=y+1

            z=z+1

        else:   

            

            if(a[x]==b[y] and a[x]>c[z]):

                 z=z+1

            if(a[x]==c[z] and a[x]>b[y]):

                  y=y+1

            if(b[y]==c[z] and b[y]>a[x]):

                   x=x+1

            if(a[x]>b[y] and a[x]>c[z]):

                   z=z+1

                   y=y+1

            if(b[y]>a[x] and  b[y]>c[z]):

                    z=z+1

                    x=x+1

            if(c[z]>b[y] and c[z]>a[x]):

                   y=y+1

                   x=x+1

 

a=[1,2,3]

b=[2,3,4]

c=[2,3,5]

fc(a,b,c)


Output

2

3

Post a Comment

0 Comments