bool isPossible(int k, int mid, const vector<int> &arr) {
int students = 1, curPage = 0;
for(pages : arr) {
if(pages > mid) return false;
if(curPage + pages > mid) {
students++;
curPage = pages;
if(students > k) return false;
} else {
curPage += pages;
}
}
return true;
}
int findPages(vector<int> &arr, int k) {
// code here
if(k > arr.size()) return -1;
int ret = -1;
int low = *max_element(arr.begin(), arr.end()); // max value in array
int high = accumulate(arr.begin(), arr.end(), 0); // sum of all elements in array
while(low <= high) {
int mid = low + (high-low) /2;
if(isPossible(k, mid, arr)) {
ret = mid;
high = mid-1;
} else {
low = mid+1;
}
}
return ret;
}