although the ans is correct test casses are no getting passed
Book allocation problem
please send your code in cb ide
my cb ide doesnot work…
this is the code that i wrote
#include
#include
using namespace std;
#define ll long long int
bool isValid(ll book[],ll n,ll k ,ll ans){
ll student=1;
ll current_pages=0;
for(int i=0;i<n;i++)
{
if(current_pages+book[i]>ans)
{
current_pages=book[i];
student++;
if(student>k)
return false;
}
else
current_pages+=book[i];
}
return true;
}
ll binarySearchBook(ll book[],ll n, ll k){
int sum=0;
ll s=0,e=0;
for(ll i=0;i<n;i++){
sum+=book[i];
s=max(s,book[i]);
}
e=sum;
int final_ans=s;
while(s<=e){
ll m=(s+e)/2;
if(isValid(book,n,k,m)){
final_ans=m;
e=m-1;
}
else{
s=m+1;
}
}
return final_ans;
}
int main(){
ll book[1000],t ,n,m,output[1000];
//cout<<“test cases”;
cin>>t;
for(int i=0;i<t;i++){
//cout<<“number of books and students”;
cin>>n>>m;
for(int j=0;j<n;j++){
//cout<<“enter pages”;
cin>>book[j];
}
}
for(int i=0;i<t;i++){
output[i] = binarySearchBook(book,n,m);
cout<<output[i]<<endl;
}
return 0;}
hey the way u are trying to generate the o/p is wrong within the first for loop which is running for t teest cases
call the binarysearchbook function store that in a variable and o/p the value
also declare a new book array for each test case and should be of size n and not 1000
please refer to the udpated code
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
According to the code u sent for each test case first the input is taken and the the output is given out and the input for the be next test case is taken and its corrosponding output is shown…But we are supposed to take the inputs for all the test cases and then we need to show the outputs for all those test cases
no no
u are not supposed to do that
It`s like u take in 1 input show o/p for that test case
then take it for the next test case
use this: this would still show the ouput at the end
still u need to show the output after computation of each i/p
ios_base::sync_with_stdio(false);
cin.tie(NULL);
okay…got it thank you