DIFRNCE - editorial

problem link:

DIFRNCE

Author:dhussa_09

Tester:dhussa_09

Translator:[dhussa_09]https://www.codechef.com/users/dhussa_09

Editorialist:[dhussa_09]https://www.codechef.com/users/dhussa_09

DIFFICULTY:
CAKEWALK

** PREREQUISITES:**
LOGIC, SORTING

PROBLEM:

The chef has received a problem from his friend who is a chemist. He has got n candies. This all candies have some sucrose level. He has to eat such candy whose sucrose level is the difference of sucrose level of candy with maximum and minimum sucrose level.

QUICK EXPLANATION:

consider,sucrose level as number stored in array.
Firstly, we will sort the array.(smallest element comes at first place and greatest goes to last place)
After that, we will take the difference of first and last element and store it in a variable. (AS, we want to search element equal to the difference of minimum and maximum element).
Finally, search the element in the array equal to the element stored in a variable.
If we got, the match then print the value of number we get otherwise print NO.

The solution in c++14 language:

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

int main() {
// your code goes here
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,finalCandey;
cin>>n;
int A[n];
for(int j=0;j<n;j++)
{
cin>>A[j];
}
sort(A, A+n);
finalCandey=A[n-1]-A[0];
int flag=0;
for(int j=0;j<n;j++)
{
if(A[j]==finalCandey)
{
cout<<finalCandey<<endl;
flag=1;
break;
}
}
if(flag==0)
cout<<“NO”<<endl;
}
return 0;
}