SOLUTION :: If wanna solve in conceptual way , it is same as allocation of books


#1

int isvalid(vector &A,int n,int height, int B)
{
long sum=0;

for(int i=0;i<n;i++)
{
    if(A[i]>height)
    sum+=(A[i]-height);
}

if(sum>=(long)B)
return true;
else
return false;

}

int Solution::solve(vector &A, int B) {
int mx=INT_MIN;
int n=A.size();
for(int i=0;i<n;i++)
mx=max(mx,A[i]);

int answer=INT_MIN;
int s=0,e=mx;

while(s<=e)
{
    int mid=s+(e-s)/2;
    
    if(isvalid(A,n,mid,B))
    answer=max((int)mid,answer),s=mid+1;
    else
    e=mid-1;
}

return answer;

}