Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
count common elements in three sorted array without using extraspace
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// } 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
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion