Book allocation problem

sir after listening to lecture i wrote this code but this is giving incorrect answer. why is it necessary to use min(ans, mid) as shown in video?

i am attaching the copy of my code here.


somebody please tell me what am i doing wrong???

Hey @pushkar24sharma :smile:, i was going through your code and noticed that you haven’t used min(ans,mid) as mentioned.
It is necessary to use min because it’s a monotonic function that means your array will be divided into two parts i.e., TTT…&FFF…
So when your min condition has min(60,80) as per given input we will choose 60 so to minimize the number of books that’s to be read by a student.
Moreover i have edited your code here : https://ide.codingblocks.com/s/328869
You can have a look.
Thank you :smile:

1 Like

@mr.encoder ok sir it is giving now correct answer but still i have that doubt. if we are using min here then in the previous question of aggressive cows we should have used max. then why we did not use max(ans, mid) there? Also in this problem the current permissible mid value for which we are returning true will definitely be smaller than the previous mid value.

please clear my doubt i am unable to understand this only point.

Hey @pushkar24sharma :grinning:,
For you I have found the best explainable video for aggressive cows here : https://youtu.be/SiE1XFhYoaA
He has explained everything while doing dry run. It might help you to find why didn’t we used , max function in aggressive cow problem :grinning:
Hope it will help, and if it does don’t forget to give this post a like :grinning:

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.

1 Like