Answer coming wrong in coding blocks compiler

#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 .

Can you please share the sample tc for which you were getting error?

The sample tc:
1
4 2
12 34 67 90

The answer is coming out to be the value which ans variable is initialized

when you are counting total no of pages you have written sum = sum + arr[ n ] it should be arr[ i ].

ooh yes got it thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.