BOOK allotment problem , my testcase is getting wrong but when i tested it manually it is correct link is https://online.codingblocks.com/player/8634/content/5276?s=1615

Question link is https://online.codingblocks.com/player/8634/content/5276?s=1615

#include
#include
using namespace std;

bool isvalidConfig(int arr[],int n,int st,int mid){
int students = 1;
int currPages = 0;
for(int i=0;i<n;i++){
if(currPages + arr[i] > mid){
students++;
if(students>st){
return false;
}

}
else{
  currPages += arr[i];
}

}
return true;

}

int pages(int arr[],int n,int st){
int s=0,e=0;
int ans=0;
for(int i=0;i<n;i++){
e += arr[i];
s=max(s,arr[i]);
}
int mid;
while(s<=e){
mid = (s+e)/2;
if(isvalidConfig(arr,n,st,mid)){
ans = mid;
e = mid-1;
}
else
s = mid+1;
}

return ans;
}

int main() {
int T;
cin>>T;
while(T–){
int n,st;
cin>>n>>st;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}

cout<<pages(arr,n,st)<<endl;

}

return 0;

}

The problem link is not accessable to me. Please add the question below.

By my understanding to your code, it is a problem similar to painter’s-partition problem.

So according to me, the error is in
if(currPages + arr[i] > mid){
students++;
if(students>st){
return false;
}
}

in this, you are supposed to make currPages =0 as we are allocating pages to the next student.

Rest of the code is correct according to me.

This is a useful link to understand the problem.