479D. Long Jumps - Codeforces Solution C++

  Problem Link : 479D. Long Jumps 


✅ C++ Solution :

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

#define ll long long int

int main()
{
	ll n,l,x,y;
	cin>>n>>l>>x>>y;
	ll arr[n+1];
	ll cum[n+1];
	cum[0]=0;
	unordered_map<ll,ll> mp;
	for(ll i=1;i<=n;i++)
	{
		cin>>arr[i];
		cum[i]=arr[i]+cum[i-1];
		mp[arr[i]]=i;
	}

	bool fx=0,fy=0;
	ll xval,yval;
	for(int i=1;i<=n;i++)
	{
		if(mp.find(x+arr[i])!=mp.end())
			fx=1;
		if(mp.find(y+arr[i])!=mp.end())
			fy=1;
	}

	if(fx && fy)
		cout<<"0";
	else if(fx)
	{
		cout<<"1\n";
		cout<<y;
	}
	else if(fy)
	{
		cout<<"1\n";
		cout<<x;
	}
	else
	{
		ll cnt=0;
		ll val1=x+y;
		ll val2=abs(x-y);
		ll mn=min(x,y);
		ll mx=max(x,y);
		for(int i=1;i<=n;i++)
		{
			if(mp.find(val1+arr[i])!=mp.end())
			{
				cout<<"1\n";
				cout<<arr[i]+x;
				exit(0);
			}
			else if(mp.find(val2+arr[i])!=mp.end())
			{

				if(arr[i]-mn>=0)
				{
					cout<<"1\n";
				    cout<<arr[i]-mn;
				}
				else if(arr[i]+mx<=l)
				{
					cout<<"1\n";
				    cout<<arr[i]+mx;
				}
				else
					continue;
				exit(0);    
			}
		}
		cout<<"2\n";
		cout<<x<<" "<<y;
	}

}

 

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