-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonIn3.cpp
49 lines (49 loc) · 1.15 KB
/
CommonIn3.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
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
vector<int> commonElements(int a[],int b[],int c[],int n1,int n2,int n3){
vector<int >ans;
int ptr1=0,ptr2=0,ptr3=0;
while (ptr1<n1 && ptr2<n2 && ptr3<n3)
{
if (a[ptr1]==b[ptr2] && b[ptr2]==c[ptr3]){
if (!ans.empty() && ans[ans.size()-1]!=a[ptr1])
ans.push_back(a[ptr1]);
if (ans.empty())
ans.push_back(a[ptr1]);
ptr1++;
ptr2++;
ptr3++;
}
else {
int mini=min(min(a[ptr1],b[ptr2]),c[ptr3]);
if (a[ptr1]==mini)
{
ptr1++;
}
else if (b[ptr2]==mini){
ptr2++;
}
else if (c[ptr3]==mini)
ptr3++;
}
}
return ans;
}
int main(){
int n1,n2,n3;
cin>>n1>>n2>>n3;
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];
}
vector<int>ans=commonElements(a,b,c,n1,n2,n3);
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
return 0;
}