Bird
Raised Fist0
DSA Cprogramming~20 mins

Allocate Minimum Pages Binary Search on Answer in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Allocate Minimum Pages Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Minimum Pages Allocation for Given Books and Students
What is the minimum number of pages allocated to a student when books are allocated optimally?
DSA C
int isPossible(int arr[], int n, int m, int mid) {
    int students = 1, pages_sum = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] > mid) return 0;
        if (pages_sum + arr[i] > mid) {
            students++;
            pages_sum = arr[i];
            if (students > m) return 0;
        } else {
            pages_sum += arr[i];
        }
    }
    return 1;
}

int findPages(int arr[], int n, int m) {
    int sum = 0, start = 0, end, result = -1;
    for (int i = 0; i < n; i++) sum += arr[i];
    end = sum;
    while (start <= end) {
        int mid = (start + end) / 2;
        if (isPossible(arr, n, m, mid)) {
            result = mid;
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return result;
}

#include <stdio.h>
int main() {
    int arr[] = {12, 34, 67, 90};
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 2;
    int result = findPages(arr, n, m);
    printf("%d\n", result);
    return 0;
}
A113
B157
C90
D101
Attempts:
2 left
💡 Hint
Think about dividing books so that the maximum pages assigned to any student is minimized.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of Binary Search in Allocate Minimum Pages
Why do we use binary search in the Allocate Minimum Pages problem?
ATo count the total number of pages in all books.
BTo sort the array of book pages before allocation.
CTo find the maximum number of students that can be allocated books.
DTo find the minimum number of pages that can be allocated to a student by searching the answer space efficiently.
Attempts:
2 left
💡 Hint
Binary search is used on the range of possible answers, not on the array itself.
🔧 Debug
advanced
2:00remaining
Identify the Error in the Feasibility Check Function
What error will occur when running this feasibility check function for Allocate Minimum Pages problem?
DSA C
int isPossible(int arr[], int n, int m, int mid) {
    int students = 1, pages_sum = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] >= mid) return 0;
        if (pages_sum + arr[i] > mid) {
            students++;
            pages_sum = arr[i];
            if (students > m) return 0;
        } else {
            pages_sum += arr[i];
        }
    }
    return 1;
}
AIt causes infinite loop due to missing increment.
BIt causes division by zero error.
CIt incorrectly returns 0 when arr[i] equals mid, causing valid allocations to fail.
DIt returns 1 even when students exceed m.
Attempts:
2 left
💡 Hint
Check the condition that compares arr[i] and mid carefully.
❓ Predict Output
advanced
1:30remaining
Output of Allocation with Single Student
What is the output of the minimum pages allocated when there is only one student for the books array?
DSA C
int arr[] = {10, 20, 30, 40};
int n = 4;
int m = 1;
int result = findPages(arr, n, m);
printf("%d\n", result);
A40
B100
C30
D10
Attempts:
2 left
💡 Hint
If only one student is there, they must read all books.
🚀 Application
expert
2:30remaining
Minimum Pages Allocation for Large Input
Given 5 books with pages [15, 17, 20, 25, 30] and 3 students, what is the minimum maximum pages allocated to any student?
A45
B42
C50
D55
Attempts:
2 left
💡 Hint
Try dividing books into 3 contiguous parts minimizing the maximum sum.

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