Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
count common elements in three sorted array without using extraspace
// } 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; 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]) { if(v.size()==0) { v.push_back(a[i]); } else if(v.back()!=a[i]) { v.push_back(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++; } } 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
TPSI Primo esercizio
RegTokIt
Simple use of function templete and namespace
in shorted array print without duplicate o/p array
reverseKNode
Breakfast
A • Potato Sacks
Best time to buy and sell stocks
aa
silnia