1148B. Born This Way - Codeforces Solution C++

  Problem Link : 1148B. Born This Way 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
int main()
{
  ll n,m,ta,tb,k;
  cin>>n>>m>>ta>>tb>>k;
  if(k>=n || k>=m)
  {
    cout<<"-1";
    exit(0);
  }
  ll A[n+1],B[m+1];
  for(ll i=1;i<=n;i++)
    cin>>A[i];
 
  for(ll i=1;i<=m;i++)
    cin>>B[i];
 
  ll ans=-1;
  ll reachB,low,mid,high,ind;
  for(ll i=0;i<=k;i++)
  {
    reachB=A[1+i]+ta;
    low=1,high=m;
    while(low<high)
    {
      mid=low+(high-low)/2;
      if(B[mid]>=reachB)
        high=mid;
      else
        low=mid+1;
    }
    if(low==1 || low==m)
    {
      if(B[low]<reachB)
      {
        cout<<"-1";
        exit(0);
      }
    }
    if(low+k-i > m)
    {
          cout<<"-1";
            exit(0);
    }
    ind=low+k-i;
    ans=max(ans,B[ind] + tb);
 
  }
  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!!