QUESTION
Aparna recently created a random number generator and now she wants Harsh to check if it works fine. She gives Harsh an array containing N numbers generated from this random number generator of hers, and two integers K and P. If the given array contains more than or equal to K numbers in the range X-P to X+P (both inclusive) for any integer X, then the random generator fails.\n\nIf for any X the generator fails then ultimately it fails.\n\nHelp Harsh in determining whether the random generator works well or not.\n\nInput:\nFirst line contains 3 space separated integers N, K and P. \nThe next line contains N integers generated by the random number generator.\n\nOutput:\nFor each Test Case Print ‘YES’ if random generator works correctly else print ‘NO’. (Quotes for clarity).
“TESTCASE_1”: “5 2 2\n2 9 6 8 4\n###—###SEPERATOR—###—\nNO”, “TESTCASE_2”: “6 3 2\n229 336 176 233 892 587\n###—###SEPERATOR—###—\nYES”, “TESTCASE_3”: “10 9 6\n-3 3 -3 3 -5 7 0 3 -4 9\n###—###SEPERATOR—###—\nNO”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0
ANSWER
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n,p,k,i,j,a,b,c;
cin>>n>>k>>p;
ll arr[n+5];
b=0;
for(i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
for(i=0;i<n-1;i++)
{
a=upper_bound(arr+i,arr+n,arr[i]+2*p)-(arr+i);
if(a>=k)
{
b=1;break;
}
//cout<<a;
}
if(b==1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
return 0;
}