Common elements

PROBLEM :

Given three arrays sorted in non-decreasing order, print all common elements in these arrays.

Input:
First line consists of T test cases. First line of every test case consists of 3 integers N1, N2 and N3, denoting the number of elements of 3 arrays. Second, Third and Forth line of every test case conisists of elements of array1, array2 and array3 respectively.

Output:
Single line output, Print the common elements of array. If not possible then print -1.

Constraints:
1<=T<=100
1<=N1,N2,N3<=1000
1<=Ai,Bi,Ci<=1000

Example:
Input:
1
6 5 8
1 5 10 20 40 80
6 7 20 80 100
3 4 15 20 30 70 80 120

Output:
20 80

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;

int greatest(int a,int b,int c){
    int temp=a>b?a:b ;
    return temp>c?temp:c ;
}

int main()
{
int t ;
cin>>t ;
while(t--){
  int x,y,z ;
  cin>>x>>y>>z ;
 
  int a1[x],a2[y],a3[z] ;
  int i ;
  bool flag=true ;
 
  for(i=0;i<x;i++)
       cin>>a1[i] ;
     
  for(i=0;i<y;i++)
       cin>>a2[i] ;
     
  for(i=0;i<z;i++)
       cin>>a3[i] ;
     
  int a,b,c ;
  a=0,b=0,c=0 ;
 
  while(a<x&&(b<y&&c<z)){
      if(a1[a]==a2[b]&&a1[a]==a3[c]){
          cout<<a1[a]<<" " ;
  flag=false ;
          a++,b++,c++ ;
      }
      else{
          if(a1[a]==a2[b]&&a1[a]>a3[c]) c++ ;
          else if(a1[a]==a3[c]&&a1[a]>a2[b]) b++ ;
          else if(a2[b]==a3[c]&&a2[b]>a1[a]) a++ ;
          else{
          if(a1[a]==greatest(a1[a],a2[b],a3[c])){
  b++,c++ ;
}
          else if(a2[b]==greatest(a1[a],a2[b],a3[c])){
  a++,c++ ;
}
          else if(a3[c]==greatest(a1[a],a2[b],a3[c])){
  a++,b++ ;
}
   }
      }
   }
   if(flag) cout<<-1 ;
   cout<<endl ;
}
return 0;
}

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Similar)
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;

int greatest(int a,int b,int c){
    int temp=a>b?a:b ;
    return temp>c?temp:c ;
}

int main()
{
int t ;
cin>>t ;
while(t--){
  int x,y,z ;
  cin>>x>>y>>z ;
 
  int a1[x],a2[y],a3[z] ;
  int i ;
  bool flag=true ;
 
  for(i=0;i<x;i++)
       cin>>a1[i] ;
     
  for(i=0;i<y;i++)
       cin>>a2[i] ;
     
  for(i=0;i<z;i++)
       cin>>a3[i] ;
     
  int a,b,c ;
  a=0,b=0,c=0 ;
 
  while(a<x&&(b<y&&c<z)){
      if(a1[a]==a2[b]&&a1[a]==a3[c]){
          cout<<a1[a]<<" " ;
          flag=false ;
          a++,b++,c++ ;
      }
      else if(a1[a]<a2[b])
           a++ ;
      else if(a2[b]<a3[c])
           b++ ;
      else
           c++ ;
   }
  if(flag) cout<<-1 ;
  cout<<endl ;
 
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )