-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommonElements.cpp
45 lines (41 loc) · 1.11 KB
/
CommonElements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
//Time Complexity-O(n1+n2+n3)
//Space Complexity-O(1)
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
{
int i=0,j=0,k=0;
vector <int> v;
while(i<n1 && j<n2 && k<n3)
{
if(A[i]==B[j] && B[j]==C[k])
{
v.push_back(A[i]);
i++;j++;k++;
}
else if(A[i]<B[j]) i++;
else if(B[j]<C[k]) j++;
else k++;
//to take only distinct elements
while(A[i]==A[i-1])i++;
while(B[j]==B[j-1])j++;
while(C[k]==C[k-1])k++;
}
return v;
}
int main()
{
int n1,n2,n3;
cin>>n1>>n2>>n3;
vector<int>ans;
int a[n1],b[n2],c[n3];
for(int i=0;i<n1;i++)
cin>>a[i];
for(int i=0;i<n2;i++)
cin>>b[i];
for(int i=0;i<n3;i++)
cin>>c[i];
ans=commonElements(a,b,c,n1,n2,n3);
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
}