Code not working plz help

#include
using namespace std;
bool condition( int a[],int n,int m,int max_page)
{
int c[m],i,j,p=0;
c[m]={0};
j=0;
for(i=0;i<n;i++)
{
c[j]+=a[i];
p++;
if(c[j]>=max_page)
{
j++;
}
if(j==m-1)
{
break;
}
}
if(p==n)
return true;
else
return false;

}
int pages(int a[],int n,int m)
{
int s,e,mid,i,ans,sum=0;
ans=-1;
s=a[n-1];
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
e=sum;
while(s<=e)
{
mid=(s+e)/2;
if(condition(a,n,m,mid))
{
ans=mid;
e=mid-1;
}
else
{
s=mid+1;
}
}
return ans;
}
int main()
{
int t,i;
cin>>t;
for(i=0;i<t;i++)
{
int n,m;
cin>>n;
cin>>m;
int a[n];
for(int j=0;j<n;j++)
{
cin>>a[i];
}
cout<<pages(a,n,m);
}
return 0;
}

Hello @tushartiwari,

Please, share your code using Online Coding Blocks IDE.
The way you have shared it may introduce many syntax errors to it.

STEPS:

  1. Paste it at https://ide.codingblocks.com/
  2. Save it there.
  3. Share the URL


(book allocation problem)

Hey @tushartiwari,

You have shared the code for Winning CB Scholarship.
Please, check it.

oh sorry

Hello @tushartiwari,

Please, refer to comments for better understanding:

Hope, this would help.
Give a like if you are satisfied.

it is not giving the correct output

Hello @tushartiwari,

This is because there are two mistakes in your main() function:

Hope, this would help.
Give a like if you are satisfied.

1 Like

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.

@tushartiwari hey I have corrected your book allocation code:


You need any other help?

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.