Book Allocation Problem

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 62 :- Book Allocation Problem

The Book Allocation Problem is a classic optimization problem related to book distribution. It is also known as the "Allocate Minimum Number of Pages" problem. The problem statement is as follows:

You are given an array of N books, where the i-th book has A[i] number of pages. You have to allocate these books to M students in such a way that each student gets a contiguous subarray of books, and the maximum number of pages assigned to a student is minimized. Each student must be allocated at least one book.

To solve this problem, we can use binary search to find the minimum number of pages that can be allocated to a student. We will set the search space to be the range from the maximum number of pages in the array to the sum of all the pages in the array. Then, we will check if it is possible to allocate the books in a way that the maximum number of pages assigned to a student is less than or equal to the mid value (the value we are currently searching for). If it is possible, we try to minimize this maximum value by moving to the left half of the search space. Otherwise, we move to the right half.

Here's the C++ implementation of the Book Allocation Problem:

cppCopy code

#include <iostream> #include <vector> #include <algorithm> bool isPossible(std::vector<int>& books, int numStudents, int maxPages) {    int students = 1;    int pagesAllocated = 0;    for (int i = 0; i < books.size(); i++) {        if (books[i] > maxPages) {            return false;        }        if (pagesAllocated + books[i] > maxPages) {            students++;            pagesAllocated = books[i];        } else {            pagesAllocated += books[i];        }    }    return students <= numStudents; } int allocateBooks(std::vector<int>& books, int numStudents) {    int n = books.size();    int totalPages = 0;    int maxPages = 0;    for (int i = 0; i < n; i++) {        totalPages += books[i];        maxPages = std::max(maxPages, books[i]);    }    int left = maxPages;    int right = totalPages;    int result = -1;    while (left <= right) {        int mid = left + (right - left) / 2;        if (isPossible(books, numStudents, mid)) {            result = mid;            right = mid - 1;        } else {            left = mid + 1;        }    }    return result; } int main() {    std::vector<int> books = {12, 34, 67, 90};    int numStudents = 2;    int minMaxPages = allocateBooks(books, numStudents);    std::cout << "Minimum maximum pages allocated: " << minMaxPages << std::endl;    return 0; }

Example Output:

yamlCopy code

Minimum maximum pages allocated: 113

In this code, the isPossible function checks if it is possible to allocate books in a way that the maximum number of pages assigned to a student is less than or equal to maxPages. The allocateBooks function uses binary search to find the minimum maximum pages that can be allocated to a student. The time complexity of this algorithm is O(N * log(sum)), where N is the number of books and sum is the sum of all pages in the array.

9. Week4 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?