Cant figure out whats wrong

https://practice.geeksforgeeks.org/problems/common-elements/0

#include<bits/stdc++.h>
using namespace std;
int main()
 {
	int t;
	cin>>t;
	while(t--){
	    long long int n,n1,n2;
	    cin>>n>>n1>>n2;
	    int x;

	    set<long long int>s,s1,s2;
	    for(int i=0;i<n;i++){
	        cin>>x;
	        s.insert(x);
	    }
	    
	   for(int i=0;i<n1;i++){
	        cin>>x;
	        s1.insert(x);
	    }
	   
	  for(int i=0;i<n2;i++){
	        cin>>x;
	        s2.insert(x);
	    }
	    
	    int count = 1;
	    
	    unordered_map<int,int>ma;
	    
	  for(auto i : s){
	      ma[i]++;

	    }
	
	   for(auto i : s1){
	      ma[i]++;
	      cout<<" "<<i<<endl;
	    }
	    
	   for(auto i : s2){
	      ma[i]++;
	    }
	    
	    for(auto i : ma){
	        if(i.second >=3){
	            count = 0;
	            cout<<i.first<<" ";
	        }
	    }
	    cout<<"\r";
	    
	    if(count){
	        cout<<-1<<"\n";
	        cout<<"\r";
	    }
	cout<<"\n";
	}
	return 0;
}

At first, since range of arr[i] is 10^18 so you can’t store it in “int” use “long long int” instead.
Now, since you have to print output in increasing order so take “map” instead of unordered map.
Also there is no need of “\n” in cout<<-1<<"\n"; since it already placed in the last line of while loop.
Also include fast input output syntax (i’ve mentioned in code).

BTW here is your correct code:

#include<bits/stdc++.h>
using namespace std;
int main()
 {
    ios_base::sync_with_stdio(false);cin.tie(NULL);  //for fast input output
	int t;
	cin>>t;
	while(t--){
	    int n,n1,n2;
	    cin>>n>>n1>>n2;
	    long long int x;

	    set<long long int>s,s1,s2;
	    for(int i=0;i<n;i++){
	        cin>>x;
	        s.insert(x);
	    }
	    
	   for(int i=0;i<n1;i++){
	        cin>>x;
	        s1.insert(x);
	    }
	   
	  for(int i=0;i<n2;i++){
	        cin>>x;
	        s2.insert(x);
	    }
	    
	    int count = 1;
	    
	    map<int,int>ma;
	    
	  for(auto i : s){
	      ma[i]++;
	    }
	
	   for(auto i : s1){
	      ma[i]++;
	      //cout<<" "<<i<<endl;
	    }
	    
	   for(auto i : s2){
	      ma[i]++;
	    }
	    
	    for(auto i : ma){
	        if(i.second >=3){
	            count = 0;
	            cout<<i.first<<" ";
	        }
	    }
	   // cout<<"\r";
	    
	    if(count){
	        cout<<-1/*<<"\n"*/;
	        //cout<<"\r";
	    }
	    
	    cout<<"\n";
	}
	
	return 0;
}
1 Like