One test case fails

#include<bits/stdc++.h>
using namespace std;
int isValidConfigration(int *p,int nos,int r,int midData){
int v = 0;
int index=-1;
int o;
int sum;
for(int i=0;i<nos;i++){
sum = 0;
for(o=index+1;o<=r;o++){

		sum = sum + p[o];
		if(sum>midData){
			sum = sum - p[o];
			index = o-1;
			break;
		}
	}
}
if(o == r+1){
	v = 1;
}
return v;

}

int BookAllocation(int *p,int r, int nos){

if(nos>r+1){
    return -1;
}

int sum=0;
int a = *max_element(p, p+r+1);
int midData;
int result;
sum = accumulate(p, p+r+1, sum);
while(a<=sum){
    midData = (a+sum)/2;
    int b = isValidConfigration(p,nos,r,midData);
	if(b==0){
	
		a=midData+1;
	}
	else if(b==1){
	    
	    result=midData;
	    sum = midData-1;
	}

}
return result;
}

int main(){

ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
int *arr = new int[n+1];
for(int i=0;i<n;i++){
	cin>>arr[i];
}
int pages=BookAllocation(arr,n-1,m);
cout<<pages<<endl;
return 0;

}

Hey @ashishranjanraju1234
Remove the first if condition in BookAllocation
if(nos>r+1){
return -1;
}
And please send your code on CB IDE from next time onwards.

If your doubt is resolved please mark it as closed.

Thank u sir all test cases passed after removing that if block
actually I wrote that if block for the cases where no of students will be greater then the no of books.

1 Like