Same approach as the question "Allocate books"


#1

bool check(vector a , int b, int m){
int sum = 0;
int n = a.size();
for(int i=0;i<n;i++){
if(sum+max(0, a[i]-m)>=b){
return true;
}
else{
sum+=max(0, a[i]-m);
}
}
return false;
}
int Solution::solve(vector &A, int B) {
int mn= INT_MAX;
int mx = 0;
for(int i=0;i<A.size();i++){
mx= max(A[i], mx);
mn= min(A[i], mn);
}
int l = mn;
int r= mx;
int ans = 0;
while(l<=r){
int m= l+(r-l)/2;
if(check(A, B, m)){
ans = max(ans , m);
l=m+1;
}
else{
r=m-1;
}

}
return ans;

}


#2

How did you know that we have to apply Allocate books solution?
Please help


#3

The question was similar in the way that we have to find an answer that range between 0 to max element value under some criteria which we have to check. Here the criteria was that the total sum of wood cut should be greater than given B value and the we have to search such value which fulfill the criteria