Just a logical solution


#1

Here the logic is simple.
The height of blade can be varied from mini height to max height.So the answer will be within range from mini to maxi.
Now simple binary search will give the answer.
Below is my code
bool isp(vectorA,int B,long long int m) //checking is it possible by taking height=m we
{ //can get B
long long int s=0;
for(int i=0;i<A.size();i++)
{ if((long long int)A[i]>m) //if height is less than m then 0.
s+=A[i]-m;}
if(s>=B)
return true;
return false;
}

int Solution::solve(vector &A, int B) {
int mini=INT_MAX,maxi=INT_MIN;
for(int i=0;i<A.size();i++)
{
mini=min(mini,A[i]);
maxi=max(maxi,A[i]);
}
long long int l=mini,h=maxi;
long long int ans=mini;
while(l<=h)
{
long long int m=(l+(h-l)/2);
if(isp(A,B,m))
{
ans=max(ans,m);
l=m+1; //Bcz we want to get maximum
}
else
h=m-1;
}
return (int)ans;
}


#2

I think the initialization of mini is wrong. It should be mini=max(0,maxi-B)