[LeetCode] Intersection of Three Sorted Arrays

1213. Intersection of Three Sorted Arrays

Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> arraysIntersection(vector<int>& A, vector<int>& B, vector<int>& C) {
int i = 0, j = 0, k = 0;
int n = A.size(), m = B.size(), o = C.size();
vector<int> res;

while(i < n and j < m and k < o) {
if(A[i] == B[j] and B[j] == C[k]) {
res.push_back(A[i]);
i++,j++,k++;
} else {
if(A[i] <= B[j] and A[i] <= C[k]) i++;
else if(B[j] <= A[i] and B[j] <= C[k]) j++;
else k++;
}

}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/01/PS/LeetCode/intersection-of-three-sorted-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.