Book Allocation Problem

Hi!
I tried my own appraoch to this problem using binary search, before i saw the tutorial.
Even though my code seems to be working fine, im not passing the hackerblocks testcases. Please go through my appraoch ( at : https://ide.codingblocks.com/s/300779) and assist me in finding what testcase/issue i am missing.

I have used a similar appraoch with the same monotonic search space and binary search algorithm.
Thanks for yout help, it is really appreciated. :smiley:

Youโ€™re not taking input for every case.You book array is same for all the cases.I think your logic is correct.
Please see my code for better understanding.
#include<bits/stdc++.h>
using namespace std;

bool check(vectorv,int n,int m,int mid){
int curr = 0;
int count = 1;
for(int i=0;i<n;i++){
if(curr+v[i]>mid){
count++;
curr = v[i];

	   if(count>m){
	  return false;
  }
	  
  }else{
	  curr = curr + v[i];
  }

}

return true;

}

int ans(vectorv,int n,int m){
int start = v[n-1];
int sum = 0;
int ans = -1;
for(int i=0;i<n;i++){
sum = sum + v[i];
}

int end = sum;
while(start<=end){
int mid = (start+end)/2;
bool check1 = check(v,n,m,mid);
if(check1==true){
ans = mid;
end = mid - 1;

}else{
start = mid + 1;
}

}

return ans;

}

int main() {
int t;
cin>>t;
while(tโ€“){
int n;
cin>>n;
int m;
cin>>m;
vectorv;
for(int i=0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}

	cout << ans(v,n,m) << endl;

}

}