Bird
Raised Fist0
DSA Cprogramming~30 mins

Allocate Minimum Pages Binary Search on Answer in DSA C - Build from Scratch

Choose your learning style10 modes available
Allocate Minimum Pages using Binary Search on Answer
📖 Scenario: Imagine you are a librarian who needs to allocate books to students. Each book has a certain number of pages. You want to distribute the books so that the student who reads the most pages has as few pages as possible. This helps keep the reading workload balanced.
🎯 Goal: You will write a program in C to find the minimum possible maximum number of pages assigned to a student using binary search on the answer.
📋 What You'll Learn
Create an array called books with these exact values: 12, 34, 67, 90
Create an integer variable called students and set it to 2
Write a function called isPossible that takes books, n, students, and mid as parameters and returns 1 if allocation is possible, otherwise 0
Use binary search between low and high to find the minimum maximum pages
Print the final minimum maximum pages allocated
💡 Why This Matters
🌍 Real World
This problem models real-world scenarios like task distribution, workload balancing, and resource allocation where fairness is important.
💼 Career
Understanding binary search on answer and allocation problems is useful for software engineers working on scheduling, load balancing, and optimization tasks.
Progress0 / 4 steps
1
Create the books array and students variable
Create an integer array called books with these exact values: 12, 34, 67, 90. Also create an integer variable called students and set it to 2.
DSA C
Hint

Use int books[] = {12, 34, 67, 90}; and int students = 2;.

2
Write the isPossible function to check allocation feasibility
Write a function called isPossible that takes parameters int books[], int n, int students, and int mid. The function returns 1 if it is possible to allocate books to students such that no student reads more than mid pages, otherwise returns 0.
DSA C
Hint

Check if any book has more pages than mid. If yes, return 0. Otherwise, count how many students are needed if max pages per student is mid.

3
Implement binary search to find minimum maximum pages
In main, implement binary search between low and high to find the minimum maximum pages that can be allocated. Use the isPossible function to check feasibility. Store the result in an integer variable called result.
DSA C
Hint

Set low to 0 and high to sum of all pages. Use binary search to update result.

4
Print the minimum maximum pages allocated
Add a printf statement to print the value of result in main.
DSA C
Hint

Use printf("%d\n", result); to print the minimum maximum pages.

Practice

(1/5)
1. What is the main goal of the Allocate Minimum Pages problem when using binary search on the answer?
easy
A. To maximize the total number of pages assigned
B. To find the average number of pages per student
C. To minimize the maximum number of pages assigned to any student
D. To assign pages randomly to students

Solution

  1. Step 1: Understand the problem goal

    The problem wants to divide book pages among students so that the student with the most pages has as few pages as possible.
  2. Step 2: Identify the binary search target

    Binary search is used to find the minimum possible maximum pages assigned to any student.
  3. Final Answer:

    To minimize the maximum number of pages assigned to any student -> Option C
  4. Quick Check:

    Goal = minimize max pages per student [OK]
Hint: Focus on minimizing the max pages per student, not total pages [OK]
Common Mistakes:
  • Confusing total pages with maximum pages per student
  • Thinking binary search finds average pages
  • Assuming random assignment solves the problem
2. Which of the following is the correct way to initialize the binary search range for the Allocate Minimum Pages problem in C?
easy
A. int low = max_page_count, high = sum_of_all_pages;
B. int low = 0, high = sum_of_all_pages;
C. int low = 0, high = max_page_count;
D. int low = sum_of_all_pages, high = max_page_count;

Solution

  1. Step 1: Determine the search boundaries

    The minimum possible max pages is at least the largest single book (max_page_count), and the maximum possible is the sum of all pages.
  2. Step 2: Set binary search range accordingly

    So, low = max_page_count and high = sum_of_all_pages is correct initialization.
  3. Final Answer:

    int low = max_page_count, high = sum_of_all_pages; -> Option A
  4. Quick Check:

    low = max single book, high = total pages [OK]
Hint: Start low at max single book pages, high at total pages sum [OK]
Common Mistakes:
  • Starting low at 0 instead of max book pages
  • Swapping low and high values
  • Using arbitrary values outside page counts
3. Given the array of pages [10, 20, 30, 40] and 2 students, what is the minimum maximum pages assigned to a student?
medium
A. 30
B. 50
C. 40
D. 60

Solution

  1. Step 1: Check possible max pages assignments

    Try to split books into 2 parts so max pages per student is minimized. Possible splits: [10,20,30] and [40] max 60; [10,20] and [30,40] max 70; [10,20,30,40] all to one student max 100.
  2. Step 2: Use binary search or reasoning

    Splitting as [10,20] and [30,40] gives max 70, but [10,20,30] and [40] gives max 60. Check if 50 is possible: [10,20] and [30,40] max 70 too high, but [10,20,30] is 60. So 50 is not possible because 40 > 50. So minimum max pages is 60.
  3. Final Answer:

    60 -> Option D
  4. Quick Check:

    Minimum max pages = 60 [OK]
Hint: Try splitting books to minimize max pages per student [OK]
Common Mistakes:
  • Choosing max single book as answer without checking splits
  • Assuming equal split always works
  • Ignoring that a student must get continuous books
4. Identify the error in this C code snippet for checking feasibility in Allocate Minimum Pages problem:
int isPossible(int arr[], int n, int m, int mid) {
    int studentCount = 1, pageSum = 0;
    for (int i = 0; i < n; i++) {
        if (pageSum + arr[i] <= mid) {
            pageSum += arr[i];
        } else {
            studentCount++;
            pageSum = arr[i];
        }
    }
    return studentCount <= m;
}
medium
A. It does not check if a single book has more pages than mid
B. It increments studentCount incorrectly
C. It should return studentCount >= m instead
D. It does not initialize pageSum to arr[0]

Solution

  1. Step 1: Analyze feasibility check logic

    The function tries to assign books to students without exceeding mid pages per student.
  2. Step 2: Identify missing check

    If a single book has pages more than mid, the function still tries to assign it, which is invalid. This case is not handled.
  3. Final Answer:

    It does not check if a single book has more pages than mid -> Option A
  4. Quick Check:

    Check single book pages <= mid [OK]
Hint: Always check if any book exceeds mid before assigning [OK]
Common Mistakes:
  • Ignoring single book size check
  • Returning wrong comparison for studentCount
  • Incorrect initialization of pageSum
5. You have books with pages [12, 34, 67, 90] and 2 students. Using binary search on answer, what is the minimum maximum pages a student can get? Explain the steps.
hard
A. 101
B. 113
C. 90
D. 67

Solution

  1. Step 1: Set binary search range

    Minimum max pages = max single book = 90, maximum = sum = 12+34+67+90=203.
  2. Step 2: Apply binary search and feasibility check

    Mid = (90+203)/2=146. Check if possible with max 146 pages: assign [12,34,67] sum=113 to student1, [90] to student2. Possible. Try lower mid.
  3. Step 3: Narrow down mid

    Mid = (90+146)/2=118. Check with 118: assign [12,34,67] sum=113 student1, [90] student2. Possible. Try lower mid.
  4. Step 4: Check mid=104

    Assign [12,34] sum=46 student1, [67,90] sum=157 student2 exceeds 104. Not possible.
  5. Step 5: Check mid=111

    Assign [12,34,67] sum=113 >111 no. Assign [12,34] sum=46 student1, [67,90] sum=157 no. Not possible.
  6. Step 6: Check mid=113

    Assign [12,34,67] sum=113 student1, [90] student2. Possible.
  7. Step 7: Final minimum max pages

    Minimum max pages = 113.
  8. Final Answer:

    113 -> Option B
  9. Quick Check:

    Minimum max pages = 113 [OK]
Hint: Use binary search between max single book and total pages [OK]
Common Mistakes:
  • Choosing max single book as answer without checking splits
  • Not testing feasibility properly
  • Confusing sum and max in binary search range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard