Allocate Minimum Pages

Last Updated : 14 Jul, 2026

Given an array arr[], where arr[i] represents the number of pages in the i-th book, and an integer k denoting the total number of students, allocate all books to the students such that:

  • Each student gets at least one book.
  • Books are allocated in a contiguous sequence.
  • The maximum number of pages assigned to any student is minimized.

If it is not possible to allocate all books among k students under these conditions, return -1.

Examples:

Input: arr[] = [12, 34, 67, 90], k = 2
Output: 113
Explanation: Books can be distributed in following ways:

  • [12] and [34, 67, 90] - The maximum pages assigned to a student is 34 + 67 + 90 = 191.
  • [12, 34] and [67, 90] - The maximum pages assigned to a student is 67 + 90 = 157.
  • [12, 34, 67] and [90] - The maximum pages assigned to a student is 12 + 34 + 67 = 113.

The third combination has the minimum pages assigned to a student which is 113.

Input: arr[] = [15, 17, 20], k = 5
Output: -1
Explanation: Since there are more students than total books, it's impossible to allocate a book to each student.

Input: arr[] = [22, 23, 67], k = 1
Output: 112
Explanation: Since there is only 1 student, all books are assigned to that student. So, maximum pages assigned to a student is 22 + 23 + 67 = 112.

Try It Yourself
redirect icon

[Naive Approach] By Iterating Over All Possible Page Limits

The approach is to search for the minimum possible value of the maximum number of pages that can be assigned to any student.

  • The lowest possible page limit is the maximum number of pages in a single book, since that book must be assigned to some student.
  • The highest possible page limit is the total number of pages across all books, which would happen if one student gets all the books.

To check if a given page limit can work, we simulate the process of assigning books to students:

  • Start with the first student and assign books one by one while the total pages remain within the current page limit.
  • If adding the next book exceeds the page limit, assign that book to the next student.
  • Repeat this process until all books have been assigned.
  • If all books can be allocated using at most k students, the current page limit is feasible.

As soon as we find the smallest page limit that allows us to allocate all books to exactly k students, we return it.

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

bool check(vector<int> &arr, int k, long long pageLimit) {

    // starting from the first student
    int cnt = 1;
    long long pageSum = 0;

    for(int i = 0; i < arr.size(); i++) {

        // if adding the current book exceeds the page
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }

    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

int findPages(vector<int> &arr, int k) {

    // if number of students are more than total books
    // then allocation is not possible
    if(k > arr.size())
        return -1;

    // minimum and maximum possible page limits
    long long minPageLimit = *max_element(arr.begin(), arr.end());
    long long maxPageLimit = accumulate(arr.begin(), arr.end(), 0LL);

    // iterating over all possible page limits
    for(long long i = minPageLimit; i <= maxPageLimit; i++) {

        // return the first page limit with we can
        // allocate books to all k students
        if(check(arr, k, i))
            return (int)i;
    }

    return -1;
}

int main() {
    vector<int> arr = {12, 34, 67, 90};
    int k = 2;
    cout << findPages(arr, k);
    return 0;
}
C
#include <stdio.h>
#include <stdbool.h>

bool check(int arr[], int n, int k, long long pageLimit) {

    // starting from the first student
    int cnt = 1;
    long long pageSum = 0;

    for(int i = 0; i < n; i++) {

        // if adding the current book exceeds the page
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }

    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

int findPages(int arr[], int n, int k) {

    // if number of students are more than total books
    // then allocation is not possible
    if(k > n)
        return -1;

    // minimum and maximum possible page limits
    long long minPageLimit = arr[0];
    long long maxPageLimit = 0;

    for(int i = 0; i < n; i++) {
        if(arr[i] > minPageLimit)
            minPageLimit = arr[i];
        maxPageLimit += arr[i];
    }

    // iterating over all possible page limits
    for(long long i = minPageLimit; i <= maxPageLimit; i++) {

        // return the first page limit with we can
        // allocate books to all k students
        if(check(arr, n, k, i))
            return (int)i;
    }

    return -1;
}

int main() {
    int arr[] = {12, 34, 67, 90};
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d\n", findPages(arr, n, k));
    return 0;
}
Java
class GFG {

    static boolean check(int[] arr, int k, long pageLimit) {

        // starting from the first student
        int cnt = 1;
        long pageSum = 0;

        for(int i = 0; i < arr.length; i++) {

            // if adding the current book exceeds the page
            // limit, assign the book to the next student
            if(pageSum + arr[i] > pageLimit) {
                cnt++;
                pageSum = arr[i];
            }
            else {
                pageSum += arr[i];
            }
        }

        // if books can assigned to less than k students then
        // it can be assigned to exactly k students as well
        return (cnt <= k);
    }

    static int findPages(int[] arr, int k) {

        // if number of students are more than total books
        // then allocation is not possible
        if(k > arr.length)
            return -1;

        // minimum and maximum possible page limits
        long minPageLimit = arr[0];
        long maxPageLimit = 0;

        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > minPageLimit)
                minPageLimit = arr[i];
            maxPageLimit += arr[i];
        }

        // iterating over all possible page limits
        for(long i = minPageLimit; i <= maxPageLimit; i++) {

            // return the first page limit with we can
            // allocate books to all k students
            if(check(arr, k, i))
                return (int)i;
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {12, 34, 67, 90};
        int k = 2;
        System.out.println(findPages(arr, k));
    }
}
Python
def check(arr, k, pageLimit):
    
    # starting from the first student
    cnt = 1
    pageSum = 0
    for pages in arr:
        
        # if adding the current book exceeds the page 
        # limit, assign the book to the next student
        if pageSum + pages > pageLimit:
            cnt += 1
            pageSum = pages
        else:
            pageSum += pages
    
    # if books can assigned to less than k students then
    # it can be assigned to exactly k students as well
    return cnt <= k

def findPages(arr, k):
    
    # if number of students are more than total books
    # then allocation is not possible
    if k > len(arr):
        return -1
    
    # minimum and maximum possible page limits
    minPageLimit = max(arr)
    maxPageLimit = sum(arr)

    # iterating over all possible page limits
    for i in range(minPageLimit, maxPageLimit + 1):
        
        # return the first page limit with we can
        # allocate books to all k students
        if check(arr, k, i):
            return i

    return -1

 
if __name__ == "__main__":
    arr = [12, 34, 67, 90]
    k = 2
    print(findPages(arr, k))
C#
using System;
using System.Linq;

class GFG {

    static bool check(int[] arr, int k, long pageLimit) {

        // starting from the first student
        int cnt = 1;
        long pageSum = 0;

        for(int i = 0; i < arr.Length; i++) {

            // if adding the current book exceeds the page
            // limit, assign the book to the next student
            if(pageSum + arr[i] > pageLimit) {
                cnt++;
                pageSum = arr[i];
            }
            else {
                pageSum += arr[i];
            }
        }

        // if books can assigned to less than k students then
        // it can be assigned to exactly k students as well
        return (cnt <= k);
    }

    static int findPages(int[] arr, int k) {

        // if number of students are more than total books
        // then allocation is not possible
        if(k > arr.Length)
            return -1;

        // minimum and maximum possible page limits
        long minPageLimit = arr.Max();
        long maxPageLimit = 0;

        for(int i = 0; i < arr.Length; i++)
            maxPageLimit += arr[i];

        // iterating over all possible page limits
        for(long i = minPageLimit; i <= maxPageLimit; i++) {

            // return the first page limit with we can
            // allocate books to all k students
            if(check(arr, k, i))
                return (int)i;
        }

        return -1;
    }

    static void Main() {
        int[] arr = {12, 34, 67, 90};
        int k = 2;
        Console.WriteLine(findPages(arr, k));
    }
}
JavaScript
function check(arr, k, pageLimit) {
    
    // starting from the first student
    let cnt = 1;
    let pageSum = 0;
    for(let i = 0; i < arr.length; i++) {
        
        // if adding the current book exceeds the page 
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }
    
    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

function findPages(arr, k) {
    
    // if number of students are more than total books
    // then allocation is not possible
    if(k > arr.length)
        return -1;
    
    // minimum and maximum possible page limits
    const minPageLimit = Math.max(...arr);
    const maxPageLimit = arr.reduce((a, b) => a + b, 0);

    // iterating over all possible page limits
    for(let i = minPageLimit; i <= maxPageLimit; i++) {
        
        // return the first page limit with we can
        // allocate books to all k students
        if(check(arr, k, i))
            return i;
    }

    return -1;
}

// Driver Code
const arr = [12, 34, 67, 90];
const k = 2;
console.log(findPages(arr, k));

Output
113

Time Complexity: O(n × (sum(arr) - max(arr))), where n is the total number of books, sum(arr) is the total number of pages in all the books and max(arr) is maximum number of pages in any book.
Auxiliary Space: O(1)

The maximum number of pages (page limit) that a student can be allocated has a monotonic property:

  • If at a page limit p, books cannot be allocated to all k students, then we need to reduce the page limit to ensure more students receive books.
  • If at a page limit p, we can allocate books to more than k students, then we need to increase the page limit so that fewer students are allocated books.

Therefore, we can apply binary search to minimize the maximum pages a student can be allocated. To check the number of students that can be allotted books for any page limit, we start assigning books to the first student until the page limit is reached, then move to the next student.

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

bool check(vector<int> &arr, int k, long long pageLimit) {

    // starting from the first student
    int cnt = 1;
    long long pageSum = 0;

    for(int i = 0; i < arr.size(); i++) {

        // if adding the current book exceeds the page
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }

    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

int findPages(vector<int> &arr, int k) {

    // if number of students are more than total books
    // then allocation is not possible
    if(k > arr.size())
        return -1;

    // search space for Binary Search
    long long lo = *max_element(arr.begin(), arr.end());
    long long hi = accumulate(arr.begin(), arr.end(), 0LL);
    int res = -1;

    while(lo <= hi) {
        long long mid = lo + (hi - lo) / 2;

        if(check(arr, k, mid)) {
            res = (int)mid;
            hi = mid - 1;
        }
        else {
            lo = mid + 1;
        }
    }

    return res;
}

int main() {
    vector<int> arr = {12, 34, 67, 90};
    int k = 2;
    cout << findPages(arr, k);
    return 0;
}
C
#include <stdio.h>
#include <stdbool.h>

bool check(int arr[], int n, int k, long long pageLimit) {

    // starting from the first student
    int cnt = 1;
    long long pageSum = 0;

    for(int i = 0; i < n; i++) {

        // if adding the current book exceeds the page
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }

    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

int findPages(int arr[], int n, int k) {

    // if number of students are more than total books
    // then allocation is not possible
    if(k > n)
        return -1;

    // maximum element of the array is minimum page limit
    long long lo = arr[0];
    for(int i = 1; i < n; i++)
        if(arr[i] > lo) lo = arr[i];

    // summation of all element is maximum page limit
    long long hi = 0;
    for(int i = 0; i < n; i++)
        hi += arr[i];

    int res = -1;

    while(lo <= hi) {
        long long mid = lo + (hi - lo) / 2;

        if(check(arr, n, k, mid)) {
            res = (int)mid;
            hi = mid - 1;
        }
        else {
            lo = mid + 1;
        }
    }

    return res;
}

int main() {
    int arr[] = {12, 34, 67, 90};
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d\n", findPages(arr, n, k));
    return 0;
}
Java
class GFG {

    static boolean check(int[] arr, int k, long pageLimit) {

        // starting from the first student
        int cnt = 1;
        long pageSum = 0;

        for(int i = 0; i < arr.length; i++) {

            // if adding the current book exceeds the page
            // limit, assign the book to the next student
            if(pageSum + arr[i] > pageLimit) {
                cnt++;
                pageSum = arr[i];
            }
            else {
                pageSum += arr[i];
            }
        }

        // if books can assigned to less than k students then
        // it can be assigned to exactly k students as well
        return (cnt <= k);
    }

    static int findPages(int[] arr, int k) {

        // if number of students are more than total books
        // then allocation is not possible
        if(k > arr.length)
            return -1;

        // search space for Binary Search
        long lo = arr[0];
        long hi = 0;

        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > lo)
                lo = arr[i];
            hi += arr[i];
        }

        int res = -1;

        while(lo <= hi) {
            long mid = lo + (hi - lo) / 2;

            if(check(arr, k, mid)) {
                res = (int)mid;
                hi = mid - 1;
            }
            else {
                lo = mid + 1;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        int[] arr = {12, 34, 67, 90};
        int k = 2;
        System.out.println(findPages(arr, k));
    }
}
Python
def check(arr, k, pageLimit):
    
    # starting from the first student
    cnt = 1
    pageSum = 0
    for pages in arr:
        
        # if adding the current book exceeds the page 
        # limit, assign the book to the next student
        if pageSum + pages > pageLimit:
            cnt += 1
            pageSum = pages
        else:
            pageSum += pages
            
    # if books can assigned to less than k students then
    # it can be assigned to exactly k students as well
    return cnt <= k

def findPages(arr, k):
    
    # if number of students are more than total books
    # then allocation is not possible
    if k > len(arr):
        return -1
    
    # search space for Binary Search
    lo = max(arr)
    hi = sum(arr)
    res = -1
    
    while lo <= hi:
        mid = lo + (hi - lo) // 2
        
        if check(arr, k, mid):
            res = mid
            hi = mid - 1
        else:
            lo = mid + 1
            
    return res

if __name__ == "__main__":
    arr = [12, 34, 67, 90]
    k = 2
    print(findPages(arr, k))
C#
using System;
using System.Linq;

class GFG {

    // function to check if books can be allocated to
    // all k students without exceeding 'pageLimit'
    static bool check(int[] arr, int k, long pageLimit) {

        // starting from the first student
        int cnt = 1;
        long pageSum = 0;

        for(int i = 0; i < arr.Length; i++) {

            // if adding the current book exceeds the page
            // limit, assign the book to the next student
            if(pageSum + arr[i] > pageLimit) {
                cnt++;
                pageSum = arr[i];
            }
            else {
                pageSum += arr[i];
            }
        }

        // if books can assigned to less than k students then
        // it can be assigned to exactly k students as well
        return (cnt <= k);
    }

    static int findPages(int[] arr, int k) {

        // if number of students are more than total books
        // then allocation is not possible
        if(k > arr.Length)
            return -1;

        // search space for Binary Search
        long lo = arr.Max();
        long hi = 0;

        for(int i = 0; i < arr.Length; i++)
            hi += arr[i];

        int res = -1;

        while(lo <= hi) {
            long mid = lo + (hi - lo) / 2;

            if(check(arr, k, mid)) {
                res = (int)mid;
                hi = mid - 1;
            }
            else {
                lo = mid + 1;
            }
        }

        return res;
    }

    static void Main() {
        int[] arr = {12, 34, 67, 90};
        int k = 2;
        Console.WriteLine(findPages(arr, k));
    }
}
JavaScript
function check(arr, k, pageLimit) {
    
    // starting from the first student
    let cnt = 1;
    let pageSum = 0;
    for(let i = 0; i < arr.length; i++) {
        
        // if adding the current book exceeds the page 
        // limit, assign the book to the next student
        if(pageSum + arr[i] > pageLimit) {
            cnt++;
            pageSum = arr[i];
        }
        else {
            pageSum += arr[i];
        }
    }
    
    // if books can assigned to less than k students then
    // it can be assigned to exactly k students as well
    return (cnt <= k);
}

function findPages(arr, k) {
    
    // if number of students are more than total books
    // then allocation is not possible
    if(k > arr.length)
        return -1;
    
    // search space for Binary Search
    let lo = Math.max(...arr);
    let hi = arr.reduce((a, b) => a + b, 0);
    let res = -1;
    
    while(lo <= hi) {
        let mid = lo + Math.floor((hi - lo) / 2);
        
        if(check(arr, k, mid)){
            res = mid;
            hi = mid - 1;
        }
        else {
            lo = mid + 1;
        }
    }
    
    return res;
}

// Driver Code
const arr = [12, 34, 67, 90];
const k = 2;
console.log(findPages(arr, k));

Output
113

Time Complexity: O(n × log(sum(arr) - max(arr))), where n is the total number of books, sum(arr) is the total number of pages in all the books and max(arr) is maximum number of pages in any book.
Auxiliary Space: O(1)

Comment