#include
#include
using namespace std;
bool isPossible(int arr[], int n, int m, int curr_min)
{
int studentUsed = 1;
int pages_reading = 0;
for (int i = 0; i < n; i++)
{
if (pages_reading + arr[i] > curr_min)
{
studentUsed++;
pages_reading = arr[i];
if (studentUsed > m)
{
return false;
}
}
else
{
pages_reading += arr[i];
}
}
return true;
}
int findPages(int arr[], int n, int m)
{
int sum = 0;
if (n < m)
{
return -1;
}
// Count no of pages
for (int i = 0; i < n; i++)
{
sum = sum + arr[n];
}
int s = arr[n - 1];
int e = sum;
int ans = INT_MAX;
while (s <= e)
{
int mid = (s + e) / 2;
if (isPossible(arr, n, m, mid))
{
ans = ans < mid ? ans : mid;
e = mid - 1;
}
else
{
// It is not possible to divide at x pages
s = mid + 1;
}
}
return ans;
}
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n, m;
cin >> n >> m;
int arr[1000];
for (int j = 0; j < n; j++)
{
cin >> arr[j];
}
cout << findPages(arr, n, m) << endl;
}
return 0;
}
This is the code written by prateek bhaiya that is giving right answer in my local g++ compiler but same code is gving wrong answer in coding blocks compiler ,in which the isPossible function is not returning true .