Problem link-Click here
I have solved this problem using MAP and SET of STL. so go through the map and set then try to see the code otherwise it's almost impossible to understand my code.
an accepted code is as follows:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
cin>>n>>m;
map<int,int> m1;
map<int,int> m2;
set<int> s;
set<int>::iterator it;
int x,y;
while(n--)
{
cin>>x;
m1[x]++;
s.insert(x);
}
while(m--)
{
cin>>y;
m2[y]++;
s.insert(y);
}
int sum=0;
for(it=s.begin();it!=s.end();it++)
{
if(m1[*it]!=m2[*it])
{
sum+= abs(m1[*it]-m2[*it]);
}
}
cout<<sum<<endl;
}
return 0;
}
I have solved this problem using MAP and SET of STL. so go through the map and set then try to see the code otherwise it's almost impossible to understand my code.
an accepted code is as follows:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
cin>>n>>m;
map<int,int> m1;
map<int,int> m2;
set<int> s;
set<int>::iterator it;
int x,y;
while(n--)
{
cin>>x;
m1[x]++;
s.insert(x);
}
while(m--)
{
cin>>y;
m2[y]++;
s.insert(y);
}
int sum=0;
for(it=s.begin();it!=s.end();it++)
{
if(m1[*it]!=m2[*it])
{
sum+= abs(m1[*it]-m2[*it]);
}
}
cout<<sum<<endl;
}
return 0;
}
No comments:
Post a Comment