hi, first of all thank you for clearing my doubts I want to give the rating but I cannot as it shows error 404
secondly I tried to implement this code on my own it works for the cases when the sum if even and for odd sum cases the answer differs by 1 could you please tell the mistake. i used a sum array to store sum of each student by checking the conditon
#include
using namespace std;
bool can_pe_point(int mid ,int n, int m,int a[])
{ int sum[m]={0};
int cnt=0;
for(int i=0;i<n;i++)
{ if(cnt<m)
{
if(sum[cnt]+a[i] <=mid)
{
sum[cnt]+=a[i];
}
else
{
cnt++;
i–;
}
}
else
break;
}
if(cnt<m)
{ cout<<mid<<" ";
return true;
}
else
return false;
}
int main()
{
// int a[]={12,34,67,90},n=4,m=2,size=4,mid;
// int l=a[size-1],u=0;
int t;
cin>>t;
for(int k=0;k<t;k++)
{
int n,m;
cin>>n>>m;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int l=a[n-1],u=0,mid;
for(int i=0;i<n;i++)
{
u=u+a[i];
}
while(l<=u)
{
mid=(l+u)/2;
if(can_pe_point(mid,n,m,a))
u=mid-1;
else
l=mid+1;
}
cout<<mid+1<<endl;
}
}