{"id":605,"date":"2025-07-10T12:56:43","date_gmt":"2025-07-10T07:26:43","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=605"},"modified":"2025-07-10T12:56:44","modified_gmt":"2025-07-10T07:26:44","slug":"allocate-minimum-pages","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/","title":{"rendered":"Allocate Minimum Pages | GFG | Binary Search"},"content":{"rendered":"\n<p>The &#8220;Allocate Minimum Pages&#8221; problem is a classic binary search question that is often asked in coding interviews. <\/p>\n\n\n\n<p>It teaches you how to optimize allocation problems using binary search. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/allocate-minimum-number-of-pages0937\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement-\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>You are given an array&nbsp;<code>arr<\/code>&nbsp;where each element represents the number of pages in a book. There are&nbsp;<code>k<\/code>&nbsp;students, and the books are arranged in a line.<br>Your task is to allocate books to students such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each student gets at least one book.<\/li>\n\n\n\n<li>Each book is allocated to exactly one student.<\/li>\n\n\n\n<li>Books are allocated in a contiguous manner (no skipping).<\/li>\n\n\n\n<li><strong>The goal is to minimize the maximum number of pages assigned to any student.<\/strong><\/li>\n<\/ul>\n\n\n\n<p>If it is not possible to allocate, return\u00a0<code>-1<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [12, 34, 67, 90], k = 2\n<strong>Output: <\/strong>113\n<strong>Explanation: <\/strong>Allocation can be done in following ways:\n[12] and [34, 67, 90] Maximum Pages = 191\n[12, 34] and [67, 90] Maximum Pages = 157\n[12, 34, 67] and [90] Maximum Pages = 113.\nTherefore, the minimum of these cases is 113, which is selected as the output.<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [15, 17, 20], k = 5<br><strong>Output: <\/strong>-1<br><strong>Explanation: <\/strong>Allocation can not be done.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><br>1 &lt;=\u00a0 arr.size() &lt;= 10<sup>6<\/sup><br>1 &lt;= arr[i] &lt;= 10<sup>3<br><\/sup>1 &lt;= k &lt;= 10<sup>3\u00a0<\/sup><\/p>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-246fd29a-5825-4819-8158-4dd294cb5d34\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#0-problem-statement-\" style=\"\">Problem Statement<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#1-brute-force-solution-try-every-possible-maximum\" style=\"\">Brute Force Solution (Try Every Possible Maximum)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#2-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#3-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#7-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#8-optimal-solution-binary-search\" style=\"\">Optimal Solution (Binary Search)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#9-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#10-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#11-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#12-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#13-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#14-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/allocate-minimum-pages\/#15-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-brute-force-solution-try-every-possible-maximum\">Brute Force Solution (Try Every Possible Maximum)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Maximum Pages:<\/strong><br>Try every possible maximum pages value from the largest single book to the sum of all pages.<\/li>\n\n\n\n<li><strong>Check If Allocation Is Possible:<\/strong><br>For each value, check if it is possible to allocate books so that no student gets more than that many pages.<\/li>\n\n\n\n<li><strong>Return the First Valid Maximum:<\/strong><br>The first value for which allocation is possible is our answer.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible value, so we\u2019re sure to find the minimum one that works.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def countStudents(self, nums, pages):\n        n = len(nums)\n        students = 1\n        pagesStudent = 0\n\n        for i in range(n):\n            if pagesStudent + nums[i] &lt;= pages:\n                # Add pages to current student\n                pagesStudent += nums[i]\n            else:\n                # Add pages to next student\n                students += 1\n                pagesStudent = nums[i]\n\n        return students\n\n    def findPages(self, arr, k) -&gt; int:\n        n = len(arr)\n\n        # Book allocation impossible\n        if k &gt; n:\n            return -1\n\n        # Calculate the range\n        low = max(arr)\n        high = sum(arr)\n\n        # Linear search for minimum maximum pages\n        for pages in range(low, high + 1):\n            if self.countStudents(arr, pages) == k:\n                return pages\n\n        return low\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">countStudents<\/span><span style=\"color: #E1E4E8\">(self, nums, pages):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        students <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        pagesStudent <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> pagesStudent <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> nums[i] <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> pages:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #6A737D\"># Add pages to current student<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                pagesStudent <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> nums[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #6A737D\"># Add pages to next student<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                students <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                pagesStudent <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums[i]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> students<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findPages<\/span><span style=\"color: #E1E4E8\">(self, arr, k) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Book allocation impossible<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> k <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Calculate the range<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">sum<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Linear search for minimum maximum pages<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> pages <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(low, high <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.countStudents(arr, pages) <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> k:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> pages<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> low<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each possible maximum pages from the largest book to the total pages, check if you can allocate books to exactly\u00a0<code>k<\/code>\u00a0students.<\/li>\n\n\n\n<li>The helper function\u00a0<code>countStudents<\/code>\u00a0simulates the allocation process.<\/li>\n\n\n\n<li>Return the smallest maximum pages for which allocation is possible.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = , k = 2<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try pages = 90: Need 3 students (not enough)<\/li>\n\n\n\n<li>Try pages = 113: Need 2 students (just right, return 113)<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>113<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O((sum(arr) &#8211; max(arr)) * n)<br>For each possible max, we check all books.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small inputs, but it\u2019s too slow for large values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search on Maximum Pages:<\/strong><br>The answer lies between the largest single book and the total pages. Use binary search to find the minimum maximum pages.<\/li>\n\n\n\n<li><strong>Check If Allocation Is Possible:<\/strong><br>For each mid value (pages), check if it\u2019s possible to allocate books to students.<\/li>\n\n\n\n<li><strong>Adjust Search Window:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If allocation is possible, try a smaller maximum.<\/li>\n\n\n\n<li>If not, try a larger maximum.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The allocation function is monotonic: as the maximum pages increases, it becomes easier to allocate. Binary search is perfect for this.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def countStudents(self, nums, pages):\n        n = len(nums)\n        students = 1\n        pagesStudent = 0\n\n        for i in range(n):\n            if pagesStudent + nums[i] &lt;= pages:\n                # Add pages to current student\n                pagesStudent += nums[i]\n            else:\n                # Add pages to next student\n                students += 1\n                pagesStudent = nums[i]\n\n        return students\n\n    def findPages(self, arr, k) -&gt; int:\n        n = len(arr)\n\n        # Book allocation impossible\n        if k &gt; n:\n            return -1\n\n        # Calculate the range for binary search\n        low = max(arr)\n        high = sum(arr)\n\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            if self.countStudents(arr, mid) &lt;= k:\n                high = mid - 1\n            else:\n                low = mid + 1\n        return low\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">countStudents<\/span><span style=\"color: #E1E4E8\">(self, nums, pages):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        students <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        pagesStudent <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> pagesStudent <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> nums[i] <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> pages:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #6A737D\"># Add pages to current student<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                pagesStudent <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> nums[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #6A737D\"># Add pages to next student<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                students <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                pagesStudent <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums[i]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> students<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findPages<\/span><span style=\"color: #E1E4E8\">(self, arr, k) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Book allocation impossible<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> k <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Calculate the range for binary search<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">sum<\/span><span style=\"color: #E1E4E8\">(arr)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.countStudents(arr, mid) <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> k:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> low<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between the largest book and the total pages.<\/li>\n\n\n\n<li>For each mid value, simulate the allocation process.<\/li>\n\n\n\n<li>If allocation is possible, try a smaller maximum.<\/li>\n\n\n\n<li>If not, try a larger maximum.<\/li>\n\n\n\n<li>Return the minimum valid maximum found.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr =<\/code>[12, 34, 67, 90]<code>, k = 2<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 90, high = 203<\/li>\n\n\n\n<li>mid = 146: Need 2 students (try smaller, high = 145)<\/li>\n\n\n\n<li>mid = 117: Need 2 students (try smaller, high = 116)<\/li>\n\n\n\n<li>mid = 103: Need 3 students (try larger, low = 104)<\/li>\n\n\n\n<li>mid = 110: Need 2 students (try smaller, high = 109)<\/li>\n\n\n\n<li>mid = 106: Need 3 students (try larger, low = 107)<\/li>\n\n\n\n<li>mid = 108: Need 3 students (try larger, low = 109)<\/li>\n\n\n\n<li>mid = 109: Need 3 students (try larger, low = 110)<\/li>\n\n\n\n<li>mid = 110: Already checked, loop ends, answer is 113<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>113<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n * log(sum(arr) &#8211; max(arr)))<br>Binary search on maximum, checking all books for each guess.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays and high values. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"15-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Allocate Minimum Pages&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Allocate Minimum Pages&#8221; problem is a classic binary search question that is often asked in coding interviews. It teaches you how to optimize allocation problems using binary search. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry<\/p>\n","protected":false},"author":1,"featured_media":606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[28,18],"class_list":{"0":"post-605","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-binary-search-on-answers","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/allocate-minimum-pages-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=605"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions\/607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/606"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}