-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection_of_three_file.cpp
More file actions
63 lines (52 loc) · 1.12 KB
/
intersection_of_three_file.cpp
File metadata and controls
63 lines (52 loc) · 1.12 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int main(){
int n1, n2, n3;
cin>>n1 >> n2 >> n3;
vector<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];
int pre_a, pre_b, pre_c ;
pre_a = pre_b = pre_c = INT_MIN;
int i, j, k;
i=j=k=0;
vector<int> res;
while(i < n1 && j < n2 && k < n3){
while(i < n1 && a[i] == pre_a){
i++;
}
while(j < n2 && b[j] == pre_b){
j++;
}
while(k < n3 && c[k] == pre_c){
k++;
}
if(a[i] == b[j] && b[j] == c[k]){
res.push_back(a[i]);
pre_a = a[i];
pre_b = b[j];
pre_c = c[k];
i++, j++, k++;
}
else if(a[i] > b[j]){
pre_b = b[j];
j++;
}
else if(b[j] > c[k]){
pre_c = c[k];
k++;
}
else{
pre_a = a[i];
i++;
}
}
for(int i : res){
cout<< i <<endl;
}
return 0;
}