Read the pages - D & C


2nd test case is not passing.
Please tell what’s wrong in the code?

Hello @a19JE0093 there were some errors in your code i have corrected them .
the way you were doing was able to give correct output for sample test case but not for all the cases.
code -https://ide.codingblocks.com/s/321908
if you feel your doubt is resolved ,please mark it as resolved.
Happy Learning !!

1 Like

I am not able to find error in isPossible() function. I think both method are same. Would you please explain?

hello @a19JE0093 yes ,sure .
what i felt wring in your code is that you were not checking
changes i made:
if(sum+pages[i]>mid){//you have to check for this condition instead of just doing it without checking .

        sum=pages[i];

        studentCount++;

        if(studentCount>m){

            return false;

        }

    }

    else{

        sum+=pages[i];

    }

what you were doing :
sum += pages[i];

    if(sum > mid){

        sum = pages[i];

        studentCount++;

    }

    if(studentCount > m)return false;

try to go throgh these both and try to read that commented line again .
you shoud check this condition if(sum+pages[i]>mid) and you are incrementing this without checking.
you have to check this first and you have to allot another student to it and then you have to start your time from that point .

And the condition you are doing in line 18 is not of use here .
and you are finding the mid and try to allot pages in that range only if it is possible store your answer in any variable there you are taking min and there is no need of it ,then you have to decrement your end by 1 otherwise you have to increment your start by 1.
these all are some errors.
i hope i have cleared your doubt .
Happy Learning !!

Oh I understood
Really you helped me a lot. Thanks a lot Sir.

you welcome @a19JE0093 .
please hit the like button if you feel your query is resolved and mark it as resolved.
Happy Learning!