Allocate Minimum Pages Binary Search on Answer in DSA C - Time & Space Complexity
We analyze how the time needed grows when we try to allocate pages to students using binary search on the answer.
We want to know how the number of steps changes as the number of books and pages increases.
Analyze the time complexity of the following code snippet.
int isPossible(int arr[], int n, int m, int mid) {
int studentCount = 1;
int pageSum = 0;
for (int i = 0; i < n; i++) {
if (pageSum + arr[i] <= mid) {
pageSum += arr[i];
} else {
studentCount++;
pageSum = arr[i];
if (studentCount > m) return 0;
}
}
return 1;
}
int allocatePages(int arr[], int n, int m) {
int start = 0, end = 0;
for (int i = 0; i < n; i++) {
if(arr[i] > start) start = arr[i];
end += arr[i];
}
int result = -1;
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;
}
This code tries to find the minimum maximum pages assigned to students by using binary search on the possible page range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop performing binary search on the page range. - Nested operation: The
forloop insideisPossiblethat checks if allocation is possible. - How many times: The binary search loop runs about
log(sum of pages)times, and each time it runs theforloop over allnbooks.
As the number of books or total pages grows, the binary search steps grow slowly, but each step checks all books.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 books, sum pages ~1000 | ~10 * log(1000) ≈ 10 * 10 = 100 |
| 100 books, sum pages ~10000 | ~100 * log(10000) ≈ 100 * 14 = 1400 |
| 1000 books, sum pages ~100000 | ~1000 * log(100000) ≈ 1000 * 17 = 17000 |
Pattern observation: The total steps grow roughly by multiplying the number of books by the logarithm of total pages.
Time Complexity: O(n log S)
This means the time grows linearly with the number of books and logarithmically with the total pages.
[X] Wrong: "The binary search alone makes this algorithm run in logarithmic time."
[OK] Correct: Each binary search step requires checking all books, so the total time multiplies the linear check by the logarithmic steps.
Understanding this time complexity helps you explain how binary search can be used on answers, a common pattern in problem solving.
It shows your ability to combine searching with validation steps efficiently.
"What if we replaced the linear check in isPossible with a more efficient method? How would that affect the time complexity?"
Practice
Allocate Minimum Pages problem when using binary search on the answer?Solution
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.Step 2: Identify the binary search target
Binary search is used to find the minimum possible maximum pages assigned to any student.Final Answer:
To minimize the maximum number of pages assigned to any student -> Option CQuick Check:
Goal = minimize max pages per student [OK]
- Confusing total pages with maximum pages per student
- Thinking binary search finds average pages
- Assuming random assignment solves the problem
Allocate Minimum Pages problem in C?Solution
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.Step 2: Set binary search range accordingly
So, low = max_page_count and high = sum_of_all_pages is correct initialization.Final Answer:
int low = max_page_count, high = sum_of_all_pages; -> Option AQuick Check:
low = max single book, high = total pages [OK]
- Starting low at 0 instead of max book pages
- Swapping low and high values
- Using arbitrary values outside page counts
[10, 20, 30, 40] and 2 students, what is the minimum maximum pages assigned to a student?Solution
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.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.Final Answer:
60 -> Option DQuick Check:
Minimum max pages = 60 [OK]
- Choosing max single book as answer without checking splits
- Assuming equal split always works
- Ignoring that a student must get continuous books
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;
}Solution
Step 1: Analyze feasibility check logic
The function tries to assign books to students without exceeding mid pages per student.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.Final Answer:
It does not check if a single book has more pages than mid -> Option AQuick Check:
Check single book pages <= mid [OK]
- Ignoring single book size check
- Returning wrong comparison for studentCount
- Incorrect initialization of pageSum
[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.Solution
Step 1: Set binary search range
Minimum max pages = max single book = 90, maximum = sum = 12+34+67+90=203.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.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.Step 4: Check mid=104
Assign [12,34] sum=46 student1, [67,90] sum=157 student2 exceeds 104. Not possible.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.Step 6: Check mid=113
Assign [12,34,67] sum=113 student1, [90] student2. Possible.Step 7: Final minimum max pages
Minimum max pages = 113.Final Answer:
113 -> Option BQuick Check:
Minimum max pages = 113 [OK]
- Choosing max single book as answer without checking splits
- Not testing feasibility properly
- Confusing sum and max in binary search range
Ready to prove it?
Pass the test and this pattern moves unknown
