Answers to: Codeforces Books Problemhttps://discuss.codechef.com/questions/105002/codeforces-books-problem<p><a href="http://codeforces.com/contest/279/problem/B">http://codeforces.com/contest/279/problem/B</a></p>
<p>Please explain the output.</p>enThu, 13 Jul 2017 13:12:43 +0530Answer by c_utkarshhttps://discuss.codechef.com/questions/105002/codeforces-books-problem/105020<p>The question asks you to find the <strong>maximum</strong> number of <strong>consecutive</strong> books that Valera can read within the given time, <strong><em>t</em></strong>.</p>
<p>Formally, you have to find the <strong>length</strong> of <strong>longest</strong> subarray such that the <strong>sum</strong> of its elements <= <strong><em>t</em></strong></p>
<p>This problem can be easily solved by using:</p>
<ol>
<li><strong>Two - Pointer</strong> approach in <strong>O(N)</strong>. </li>
<li><strong>Binary Search</strong> over the cumulative sum in <strong>O(N log N)</strong>.</li>
</ol>
<p>You can refer to my solutions if get stuck.</p>
<p><strong>Two Pointer</strong>: <a href="http://codeforces.com/contest/279/submission/28493897">http://codeforces.com/contest/279/submission/28493897</a></p>
<p><strong>Binary Search</strong>: <a href="http://codeforces.com/contest/279/submission/28493182">http://codeforces.com/contest/279/submission/28493182</a></p>c_utkarshThu, 13 Jul 2017 13:12:43 +0530https://discuss.codechef.com/questions/105002/codeforces-books-problem/105020Answer by dishant_18https://discuss.codechef.com/questions/105002/codeforces-books-problem/105012<p>The output is the maximum no. of books the person can read!<br>
Consider case 1:<br>
You have t=5. Now if u start reading from book 1(i.e. one with t=3), you can only read 2 books(t=3,t=1) as total time for reading the books will be 4 units and then you can't read next book as it costs 2 units of time and 4+2>5!!<br>
So, you start with the second book and go till the end. This will make it possible to read 3 books(i.e. one with t=1,t=2 and t=1). This is the maximum no. of books the person can read provided the constraints. </p>
<p>Hope this helps :)</p>dishant_18Thu, 13 Jul 2017 12:23:14 +0530https://discuss.codechef.com/questions/105002/codeforces-books-problem/105012