-
Notifications
You must be signed in to change notification settings - Fork 2
/
common_elements_in_3_sorted_array.cpp
54 lines (47 loc) · 1.2 KB
/
common_elements_in_3_sorted_array.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
50
51
52
53
54
class Solution
{
public:
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
{
vector<int> res;
int i = 0, j = 0, k = 0;
while(i < n1 and j < n2 and k < n3)
{
while(i > 0 and i < n1 and A[i - 1] == A[i])
{
i++;
}
while(j > 0 and j < n2 and B[j - 1] == B[j])
{
j++;
}
while(k > 0 and k < n3 and C[k - 1] == C[k])
{
k++;
}
if(i < n1 and j < n2 and k < n3)
{
if(A[i] == B[j] and B[j] == C[k])
{
res.push_back(A[i]);
i++;
j++;
k++;
}
else if(A[i] < B[j])
{
i++;
}
else if(B[j] < C[k])
{
j++;
}
else
{
k++;
}
}
}
return res;
}
};