-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19) CommonElementsThreeArray.cpp
137 lines (121 loc) · 3.08 KB
/
19) CommonElementsThreeArray.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
bool binarySearch(int arr[], int n, int element){
int low = 0, high = n-1;
while(low <= high){
int mid = (low + high) /2;
if(arr[mid] == element){
return true;
}else if(arr[mid] > element){
high = mid - 1;
}else{
low = mid + 1;
}
}
return false;
}
// find common method
void findCommon(int a[], int b[], int c[], int n1, int n2, int n3){
for(int j=0; j<n1; j++){
if(j != 0 && a[j] == a[j-1]){
continue;
}
if(binarySearch(b, n2, a[j]) && binarySearch(c, n3, a[j])){
cout<<a[j]<<" ";
}
}
}
//main method
int main(){
int a[] = {1,2,3,4};
int b[] = {2,3,4,5,6,7};
int c[] = {3,4,5,6,7,8};
int i = 0, j=0, k=0;
int n1 = sizeof(a)/sizeof(a[0]);
int n2 = sizeof(b)/sizeof(b[0]);
int n3 = sizeof(c)/sizeof(c[0]);
/*
simple approach
while(i<n1 && j<n2 && k<n3){
if(a[i] == b[j] && b[j] == c[k]){
cout<<a[i]<<" ";
i++;
j++;
k++;
}else if(a[i] < b[j]){
i++;
}else if(b[j] < c[k]){
j++;
}else{
k++;
}
}*/
findCommon(a,b,c,n1,n2,n3);
return 0;
}
/*
using vector solution
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3){
vector<int> res, vect;
int i=0, j=0,k=0;
int cnt = 0;
while(i<n1 && j<n2 && k<n3){
if(A[i] == B[j] && B[j] == C[k]){
vect.push_back(A[i]);
i++;
j++;
k++;
}else{
int cnt = min(A[i], min(B[j], C[k]));
if(cnt == A[i]){
i++;
}else if(cnt == B[j]){
j++;
}else{
k++;
}
}
}
//push vecter values into the result vector
for(int x=0; x<vect.size(); x++){
if(vect[x] != vect[x+1]){
res.push_back(vect[x]);
}
}
return res;
}
};
//{ Driver Code Starts.
int main ()
{
int t; cin >> t;
while (t--)
{
int n1, n2, n3;
cin >> n1 >> n2 >> n3;
int A[n1];
int B[n2];
int 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];
Solution ob;
vector <int> res = ob.commonElements (A, B, C, n1, n2, n3);
if (res.size () == 0)
cout << -1;
for (int i = 0; i < res.size (); i++)
cout << res[i] << " ";
cout << endl;
}
}
// } Driver Code Ends
*/