Common Elements in 3 Sorted Arrays

Problem Link: https://practice.geeksforgeeks.org/problems/common-elements1132/1


class Solution:
    def commonElements (self,A, B, C, n1, n2, n3):
        # your code here
        i=0
        j=0
        k=0
        ans=[]
        while(i<n1 and j<n2 and k<n3):
            if(A[i]==B[j] and B[j]==C[k]):
                ans.append(A[i])
                i+=1
                j+=1
                k+=1
                
                xx=A[i-1]
                while(i<n1 and xx==A[i]):
                    i+=1
                
                yy=B[j-1]
                while(j<n2 and yy==B[j]):
                    j+=1
                
                zz=C[k-1]
                while(k<n3 and zz==C[k]):
                    k+=1
                
            elif(A[i]<B[j]):
                i+=1
            elif(B[j]<C[k]):
                j+=1
            else:
                k+=1
        return ans

Last updated