< Back to forum

### MINEAT March Challenge

my code is not giving the correct outputs, bt i didn't find any logical error. Please tell me where should i m make the corrections ?

My Code:

``````#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
long long int n,h,arr[100005],i,k,sum=0,mid;
cin>>t;
while(t--)
{	cin>>n>>h;
sum=0;
for(i=0;i<n;i++)
{cin>>arr[i];
sum+=arr[i];}
int r=sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+r);
if(h==n)
k=arr[n-1];
else
{	mid=sum/h;
for(i=0;i<n;i++)
{	if(arr[i]<=mid)
sum-=arr[i];
else
break;
}
while(1)
{	if((h-i)*mid>=sum)
break;
else
mid+=1;
}
k=mid;
}
cout<<k<<"\n";
}
return 0;
}``````

Asked by: Samrat_De on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.

Preview

##### Enter your comment details below:

Preview

Firstly you are declaring a fixed sized array arr with 100005 elements and then sorting using sort funtion in STL.

``````int r=sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+r);``````

Suppose the value of n for a particular testcase is 10,but the above code sorts  all 100005 elements so the resultant array contains garbage values.

first parameter to sort function must be the  starting address of the array and second parameter ,the end address upto which sorting is to be performed on the array.

``sort(arr,arr+n);``

Before using any STL function google and read about it don't just blindly copy code without understanding.

Coming to the Binary search implementation of yours ,i don't think you are clear with the concept.

you can view my solution here: https://www.codechef.com/viewsolution/17845125

Shubham_Kumar_Gupta last updated on April 7, 2019, 6:34 p.m.

##### Instruction to write good question
1. 1. Write a title that summarizes the specific problem
2. 2. Pretend you're talking to a busy colleague
3. 3. Spelling, grammar and punctuation are important!