Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
A comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with the nut to see which one is bigger/smaller.
Here is a simple and easy solution using a map and vector with execution time is 0.01s.
#include <bits/stdc++.h>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<char> v={'!' ,'#' ,'$' , '%' ,'&' ,'*' ,'@' , '^' , '~' };
int k=2;
while(k--)
{
map<char,int> mp;
char ch;
for(int i=0;i<n;++i)
{
cin>>ch;
mp[ch]++;
}
for(int i=0;i<9;++i)
{
int l=mp[v[i]];
if(l--)
{
cout<<v[i]<<" ";
}
}
cout<<endl;
}
}
return 0;
}