Book allocation problem

please tell where my code is incorrect as the test case is not getting passed
code is as follows:-

#include
using namespace std;

bool isvalid(int books[],int n,int m,int mid){
int pages=0;
int students=1;
for(int i=0;i<n;i++){

	if(pages+books[i]>mid){
		pages=books[i];
		students++;
		if(students>m){
			return false;
		}
	}
    else{
        pages+=books[i];
    }
}
return true;

}

int main() {
int t;
cin>>t;

for(int y=0;y<t;y++){
	int n,m;
	cin>>n>>m;
	// initialisation and declaration of the array named books
	int books[n];
	for(int i=0;i<n;i++){
		cin>>books[i];
	}
	// to calc sum of pages
	int sumPages=0;
	for(int i=0;i<n;i++){
		sumPages+=books[i];
	}

	int s=0;
	int e=sumPages;
	int finalans=0;
	// binary search
	while(s<=e){
		int mid=(s+e)/2;
		if(isvalid(books,n,m,mid)){
			finalans=mid;
			e=mid-1;
		}
		else{
			s=mid+1;

		}
	}
	cout<<finalans<<endl;
}
return 0;

}

Kriti, your code is perfectly fine, but you only need to include one condition in your code, in bool isValid function, i.e
if(books[i]>mid)
{
return false;
}
I have edited your code, try to submit it now…

can u please explain the concept behind rhat

The concept here is that, at any point if the value of any particular book becomes greater than the mid value which is the required value at any point of time, then it means that You cannot include that particular book and hence it would return false…