Shaky has N (1<=N<=50000) candy boxes each of them contains a non-zero number of candies (between 1 and 1000000000). Shakey want to distibute these candies among his K (1<=K<=1000000000) IIIT-Delhi students. He want to distibute them in a way such that:
All students get equal number of candies.
All the candies which a student get must be from a single box only.
As he want to make all of them happy so he want to give as many candies as possible. Help Shaky in finding out what is the maximum number of candies which a student can get.
Input
First line contains 1<=T<=20 the number of test cases. Then T test cases follow. First line of each test case contains N and K. Next line contains N integers, ith of which is the number of candies in ith box.
Output
For each test case print the required answer in a seperate line.
Example
Input:
2
3 2
3 1 4
4 1
3 2 3 9
Output:
3
9
Someone please explain how to use binary search here.
Given array: 3 1 4
After sorting: 1 3 4
N = 3 & K = 2;
1st iteration: mid = (0+3)/2 = 1;
a[mid] = 3 = val;
Now make a check whether val satisfies our condition or not?
Here is the function for the same:
bool func(ll val,ll k,ll n)
{
if(k==1)
return true;
ll sum=0;
for(ll i=n-1;i>=0;i--)
{
sum +=(a[i]/val);
}
if(sum >= k)
return true;
else
return false;
}
Here is the code explanation:
As we looping from N-1 to 0,our array becomes 4 3 1, so not to check whether one/more child can get candy out of box or not,sum = sum+a[i]/val;so now