0
0
DSA C++programming~10 mins

Allocate Minimum Pages Binary Search on Answer in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Allocate Minimum Pages Binary Search on Answer
Set low = max(pages array)
Set high = sum(pages array)
While low <= high
Calculate mid = (low + high) / 2
Check if allocation possible with mid
high = mid-1
Repeat loop
Return low as minimum pages
We use binary search on the range of possible maximum pages to allocate, checking feasibility each time, narrowing down to the minimum possible maximum.
Execution Sample
DSA C++
int allocatePages(int pages[], int n, int students) {
  int low = *max_element(pages, pages + n), high = 0;
  for (int i = 0; i < n; i++) high += pages[i];
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (canAllocate(pages, n, students, mid)) high = mid - 1;
    else low = mid + 1;
  }
  return low;
}
This code finds the minimum maximum pages per student by binary searching between max single book pages and total pages.
Execution Table
StepOperationmid (max pages)Allocation Possible?lowhighVisual State
1Initialize low and highN/AN/Amax pages = 90sum pages = 250[90, 40, 30, 50, 40]
2Calculate mid170Check allocation with max 170 pages90250[90, 40, 30, 50, 40]
3Allocation check170Yes90250Allocate: Student1:90+40+30=160, Student2:50+40=90
4Update high170N/A90169[90, 40, 30, 50, 40]
5Calculate mid129Check allocation with max 129 pages90169[90, 40, 30, 50, 40]
6Allocation check129Yes90169Allocate: Student1:90, Student2:40+30+50=120, Student3:40
7Update high129N/A90128[90, 40, 30, 50, 40]
8Calculate mid109Check allocation with max 109 pages90128[90, 40, 30, 50, 40]
9Allocation check109No90128Allocate: Student1:90, Student2:40+30=70, Student3:50+40=90 (3 students needed, but only 2 available)
10Update low109N/A110128[90, 40, 30, 50, 40]
11Calculate mid119Check allocation with max 119 pages110128[90, 40, 30, 50, 40]
12Allocation check119Yes110128Allocate: Student1:90, Student2:40+30=70, Student3:50+40=90
13Update high119N/A110118[90, 40, 30, 50, 40]
14Calculate mid114Check allocation with max 114 pages110118[90, 40, 30, 50, 40]
15Allocation check114No110118Allocate: Student1:90, Student2:40+30=70, Student3:50+40=90 (3 students needed, only 2 available)
16Update low114N/A115118[90, 40, 30, 50, 40]
17Calculate mid116Check allocation with max 116 pages115118[90, 40, 30, 50, 40]
18Allocation check116Yes115118Allocate: Student1:90, Student2:40+30=70, Student3:50+40=90
19Update high116N/A115115[90, 40, 30, 50, 40]
20Calculate mid115Check allocation with max 115 pages115115[90, 40, 30, 50, 40]
21Allocation check115No115115Allocate: Student1:90, Student2:40+30=70, Student3:50+40=90 (3 students needed, only 2 available)
22Update low115N/A116115[90, 40, 30, 50, 40]
23Exit loopN/Alow > high116115[90, 40, 30, 50, 40]
💡 Loop ends when low (116) > high (115), minimum max pages is 116
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 10After Step 11After Step 14After Step 16After Step 19After Step 22Final
low90909090110110110115115116116
high250250169128128128118118115115115
midN/A170129109109119114116116115N/A
Key Moments - 3 Insights
Why do we start low with the maximum single book pages?
Because no student can be assigned less than the largest book's pages, starting lower than that is impossible. See Step 1 in execution_table.
Why does the allocation check sometimes require more students than available?
If mid is too small, books can't be grouped within that max pages limit, so more students are needed. This is shown in Steps 9 and 15 where allocation fails.
Why do we update low to mid + 1 when allocation is not possible?
Because mid is too small to allocate all books, we must try a larger max pages value. See Steps 10 and 16 for this update.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 6, is allocation possible with mid = 129?
AYes, allocation is possible with 3 students
BNo, allocation is not possible
CYes, allocation is possible with 2 students
DNo, allocation requires more than 3 students
💡 Hint
Check the 'Allocation Possible?' column at Step 6 in execution_table
At which step does the condition low <= high become false, ending the loop?
AStep 22
BStep 23
CStep 21
DStep 20
💡 Hint
Look at the exit_note and Step 23 in execution_table
If the largest book had 100 pages instead of 90, what would be the new initial low value?
A90
B250
C100
D170
💡 Hint
Initial low is max pages of a single book, see Step 1 in execution_table
Concept Snapshot
Allocate Minimum Pages using Binary Search:
- Set low = max pages of single book
- Set high = sum of all pages
- While low <= high:
   - mid = (low + high) / 2
   - Check if allocation possible with mid
   - If yes, high = mid - 1
   - Else, low = mid + 1
- Return low as minimum max pages
Full Transcript
This concept uses binary search on the answer space to find the minimum maximum pages that can be allocated to students. We start with low as the largest single book pages and high as the total pages. We repeatedly check if allocation is possible with mid as max pages. If yes, we try smaller max by moving high down. If no, we increase low. The process continues until low exceeds high, and low is the answer.