fahimkamal63

Common Elements

Apr 22nd, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. long long int remove_duplicate(long long int a[], long long int n){
  5.     long long int i, j = 0;
  6.     for(i = 0; i < n-1; i++){
  7.         if(a[i] != a[i+1]){
  8.             a[j++] = a[i];
  9.         }
  10.     }
  11.     a[j++] = a[n-1];
  12.     return j;
  13. }
  14.  
  15. int main(){
  16.     int t; cin >> t;
  17.     while(t--){
  18.         long long int n1, n2, n3; cin >> n1 >> n2 >> n3;
  19.         long long int a[n1], b[n2], c[n3], i;
  20.         //  Taking input
  21.         for(i = 0; i < n1; i++) cin >> a[i];
  22.         for(i = 0; i < n2; i++) cin >> b[i];
  23.         for(i = 0; i < n3; i++) cin >> c[i];
  24.  
  25.         n1 = remove_duplicate(a, n1);
  26.         n2 = remove_duplicate(b, n2);
  27.         n3 = remove_duplicate(c, n3);
  28.  
  29.         //  Show array
  30.         /*
  31.         for(i = 0; i < n1; i++) cout << a[i] << ' ';
  32.         cout << endl;
  33.         */
  34.         long long int j = 0, k = 0, flag = 1; i = 0;
  35.         while(i < n1 && j < n2 && k < n3){
  36.             if(a[i] == b[j] && b[j] == c[k]){
  37.                 cout << a[i] << ' ';
  38.                 i++; j++; k++;
  39.                 flag = 0;
  40.             }
  41.             if(a[i] < b[j]) i++;
  42.             else if(b[j] < c[k]) j++;
  43.             else k++;
  44.         }
  45.         if(flag) cout << "-1";
  46.         cout << endl;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment