PROBLEM LINK:
Author: Anjali Jha
Tester: Sunita Sen
DIFFICULTY:
CAKEWALK
PREREQUISITES:
None
PROBLEM:
There are N integers in an array. The kids are happy if the difference between the maximum and minimum numbers in the array is less than X.
EXPLANATION:
The code involves only finding out the maximum and minimum integers in the array and checking if their difference is strictly less than X.
TIME COMPLEXITY:
The time complexity will be O(N)
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve() {
ll n,x,mx=LONG_MIN,mn=LONG_MAX;
string ans="NO";
cin>>n>>x;
while(n--) {
ll k;
cin>>k;
if(k>=mx)
mx=k;
if(k<=mn)
mn=k;
}
if(mx-mn<x)
ans="YES";
cout<<ans<<endl;
}
int main(){
ios ::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
solve();
return 0;
}