Chef and Employment test
**Note : * It is guaranteed that n+k is an odd number and also k < n.
So it is obvious that the median will be a number having index number between 0 and n-1.
And also the median of the whole array will be at index (n+k)/2 .
First of all sort the given array
We have to add numbers such that we have the maximum medium .
There can be two cases :
(1) Adding numbers greater than the number at index (n+k)/2 .
(2) Adding numbers less than the number at index (n+k)/2 .
If we add numbers less than the number at index (n+k)/2 , it will shift that number away from the median and the new median will be a lesser number .
So we should only add number greater than number at index (n+k)/2 .
So the answer by default becomes the number at index (n+k)/2 in the original array.
Chef and Weird Queries
A simple observation is that b can’t be greater than 700 so all the values of A , having , number of B values that satisfy the equation can be at max 700 even if there are a lot of possible values . So if the number of B that satisfy the equation for a particular A are greater than 700 , we only consider 700 pairs for that A .
Building upon this ,
if y-700 <=0
this means that there are no such values which for a particular A , have number of B >700 . So we only simply count the number of B for every value of A
//Here i represents A
for(register long long int i=1;i*i<=y;i++)
{
if((y-i*i)<=700)
ans=ans+(y-i*i);
else
ans=ans+700;
}
else
upto the square root of y-700 , for all A , number of B are greater than 700 so we add them to our answer
a=(long long int)(sqrt(y-700));
ans=ans+(long long int)(700*a);
And then from (long long int)(sqrt(y-700))+1 until a^2 <=y , we individually add B for every value of A.
for(long long int i=(a)+1;i*i<=y;i++)
{
if((y-i*i)<=700)
ans=ans+(y-i*i);
}
**Link : ** https://www.codechef.com/viewsolution/15931204