-
Notifications
You must be signed in to change notification settings - Fork 11
/
CS_29_CommonElementsIn3SortedArrays.cpp
71 lines (66 loc) · 1.84 KB
/
CS_29_CommonElementsIn3SortedArrays.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to find common elements in three arrays.
// tc: O(n1 + n2 + n3) and sc: O(1) because we are not using any extra space except the answer vector
vector<int> commonElements(vector<int> &A, vector<int> &B,
vector<int> &C) {
//code here.
int i=0,j=0,k=0;
int n1=A.size(),n2=B.size(),n3=C.size();
vector<int> ans;
int last = 0;
while(i<n1 && j<n2 && k<n3){
if(A[i] == B[j] && B[j] == C[k] && A[i] != last){
ans.push_back(A[i]);
last = A[i];
i++; j++; k++;
}
else if(min({A[i], B[j], C[k]}) == A[i]) i++;
else if(min({A[i], B[j], C[k]}) == B[j]) j++;
else k++;
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr, brr, crr;
string input1;
getline(cin, input1);
stringstream ss1(input1);
int number1;
while (ss1 >> number1) {
arr.push_back(number1);
}
string input2;
getline(cin, input2);
stringstream ss2(input2);
int number2;
while (ss2 >> number2) {
brr.push_back(number2);
}
string input3;
getline(cin, input3);
stringstream ss3(input3);
int number3;
while (ss3 >> number3) {
crr.push_back(number3);
}
Solution ob;
vector<int> res = ob.commonElements(arr, brr, crr);
if (res.size() == 0)
cout << -1;
for (int i = 0; i < res.size(); i++)
cout << res[i] << " ";
cout << endl;
}
}
// } Driver Code Ends