Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if allocation is possible with given max pages.
DSA C
int isPossible(int arr[], int n, int m, int mid) {
int studentCount = 1;
int pageSum = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > mid) return 0;
if (pageSum + arr[i] > [1]) {
studentCount++;
pageSum = arr[i];
if (studentCount > m) return 0;
} else {
pageSum += arr[i];
}
}
return 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with m or n instead of mid.
Not resetting pageSum when allocating to next student.
✗ Incorrect
We check if adding current book pages exceeds the mid (max pages allowed). So we compare pageSum + arr[i] > mid.
2fill in blank
mediumComplete the code to find the sum of all pages in the array.
DSA C
int sum = 0; for (int i = 0; i < n; i++) { sum += [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding index i instead of arr[i].
Adding n or sum instead of arr[i].
✗ Incorrect
We add the pages of each book arr[i] to sum.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loop.
DSA C
while (start <= end) { int mid = start + (end - start) / 2; if (isPossible(arr, n, m, mid)) { result = mid; end = [1]; } else { start = mid + 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting end = mid + 1 causes infinite loop or wrong search.
Using start - 1 or end + 1 causes invalid ranges.
✗ Incorrect
When allocation is possible, we try to minimize max pages by moving end to mid - 1.
4fill in blank
hardFill both blanks to initialize start and end for binary search.
DSA C
int start = [1]; int end = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting start to 0 instead of maxPage.
Setting end to n instead of sumPages.
✗ Incorrect
Start is maxPage (max single book pages), end is sumPages (sum of all pages).
5fill in blank
hardFill all three blanks to complete the main function for Allocate Minimum Pages.
DSA C
int allocateMinimumPages(int arr[], int n, int m) {
int maxPage = 0, sumPages = 0;
for (int i = 0; i < n; i++) {
maxPage = (arr[i] > maxPage) ? [1] : maxPage;
sumPages += arr[i];
}
int start = [2];
int end = [3];
int result = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (isPossible(arr, n, m, mid)) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using maxPage instead of arr[i] in ternary operator.
Setting start or end incorrectly.
✗ Incorrect
maxPage updates with arr[i] if arr[i] is greater; start = maxPage; end = sumPages.
