489B. BerSU Ball - Codeforces Solution C++

  Problem Link : 489B. BerSU Ball 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n,m,x;
	cin>>n;
	priority_queue<int,vector<int>,greater<int> > boys;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		boys.push(x);
	}
	cin>>m;
	priority_queue<int,vector<int>,greater<int> > girls;
	for(int i=0;i<m;i++)
	{
		cin>>x;
		girls.push(x);
	}
	int ans=0;
	while(!boys.empty())
	{
		int boy=boys.top();
		boys.pop();

		while(!girls.empty())
		{
			x=girls.top();
			girls.pop();
			if(x==boy-1 || x==boy || x==boy+1)
			{
				ans++;

				break;
			}
			if(x>boy)
			{
				girls.push(x);
				break;
			}
		}


	}
	cout<<ans;


}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!