My issue
it is failing 4 2 2 (2 1 4 2) case
it should be showing “no” but is showing “yes”.
My code
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
cin>>t;
while (t--)
{int n,k,l;
cin>>n>>k>>l;
l=l--;
int a[n];
for (int i=0;i<n;i++)
{cin>>a[i];}
int u=a[n-1];
sort(a,a+n);
int e=a[n-1];
int q=u+(k*l);
if (q==e){cout<<"no";}
else if (u==e){cout<<"no";}
else if (q>e ){cout<<"yes";}
else if (u>e){cout<<"yes";}
else {cout<<"no";}
cout<<endl;
}
return 0;
}
Problem Link: NODRUGS Problem - CodeChef
@sakshammaitri
There are one mistake in l=l-- that u have done and one more in for some base cases like what is n==1 and all so u generalize it i have coded it out in simpler way to cover all cases hope u will get it.
include
include
using namespace std;
int main() {
int t;
cin>>t;
while (t–)
{int n,k,l;
cin>>n>>k>>l;
l–;
int a[n];
for (int i=0;i<n;i++)
{cin>>a[i];}
int u=a[n-1];
//sort(a,a+n);
int ch=0;
int q=u+(k*l);
for(int i=0;i<n-1;i++)
{
if(u<=a[i])
{
ch=1;
break;
}
}
if(ch)
{
ch=0;
for(int i=0;i<n-1;i++)
{
if(q<=a[i])
{
ch=1;
break;
}
}
if(ch)
cout<<“No”;
else
cout<<“Yes”;
}
else
cout<<“Yes”;
cout<<endl;
}
return 0;
}
1 Like