Going wrong for all test cases

#include
using namespace std;
bool studentscanplace(int a[],int mid,int stud,int n){
int l=1;
int cpages=0;
for(int i=0;i<n;i++){
if(cpages+a[i]>mid){
l+=1;
cpages=a[i];
if(l>stud){
return false;
}
}
else{
cpages+=a[i];
}
}
return true;
}

int main(){
int t,ans;
cin>>t;
while(t–){
int n,m,s=0;
cin>>n>>m;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
s+=a[i];
}
int start=0,end=s;
while(start<=end){
int mid=(start+end)/2;
bool students=studentscanplace(a,mid,m,n);
if(students){
ans=mid;
end=mid-1;
}
else{

			start=mid+1;
		}
	}
	
 cout<<ans;
}

}
please tel where am i going wrong

print ans for all testcase in seperate line

if this not work
send me link of code i will see

Modified Code

  1. print all testcase in separate line
  2. s=a[n-1] not 0

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

1 Like

still all test cases are not passed