Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Common elements in 3 sorted vectors with extra space
#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) { //code here. vector<int>v; set<int>s; int i=0;int j=0;int k=0; int c1=0; while(i<n1 && j<n2 && k<n3) { if(a[i]==b[j] && a[i]==c[k]) { s.insert(a[i]); i++,j++,k++; } else if(a[i]<=b[j] && a[i]<=c[k]) { i++; } else if(c[k]<=b[j] && c[k]<=a[i]) { k++; } else if(b[j]<=a[i] && b[j]<=c[k]) { j++; } } for(auto it=s.begin();it!=s.end();it++) { v.push_back(*it); } return v; } }; // { 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
run
|
edit
|
history
|
help
0
Beadandó
P1
Estocasticos V1.1
O
cache 内存消耗
c++ mouse
First and last Occurence of an Element
finding counting multiple no that became a no
c2p
copy_if c++98