546C. Soldier and Cards - Codeforces Solution C++

  Problem Link : 546C. Soldier and Cards 


✅ C++ Solution :

 
#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin>>n;
	int k1,k2,x,y;
	cin>>k1;
	queue<int> first;
	for(int i=1;i<=k1;i++)
	{
		cin>>x;
		first.push(x);
	}
	cin>>k2;
	queue<int> second;
	for(int i=1;i<=k2;i++)
	{
		cin>>x;
		second.push(x);
	}
	int ans=0;

	while(!first.empty() && !second.empty() && ans<100000)
	{
		x=first.front();
		first.pop();
		y=second.front();
		second.pop();
		ans++;
		if(x<y)
		{
			second.push(x);
			second.push(y);
		}
		else
		{
			first.push(y);
			first.push(x);
		}
	}

	if(ans>=100000)
		cout<<"-1";
	else
	{
		if(first.empty())
		{
			cout<<ans<<" 2";
		}
		else
			cout<<ans<<" 1";
	}

}

 

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!!