0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 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
What is the output of the following TypeScript code that finds the minimum number of pages allocated to a student using binary search?
DSA Typescript
function isPossible(books: number[], students: number, maxPages: number): boolean {
  let requiredStudents = 1;
  let currentSum = 0;
  for (const pages of books) {
    if (pages > maxPages) return false;
    if (currentSum + pages > maxPages) {
      requiredStudents++;
      currentSum = pages;
      if (requiredStudents > students) return false;
    } else {
      currentSum += pages;
    }
  }
  return true;
}

function allocateMinimumPages(books: number[], students: number): number {
  let start = Math.max(...books);
  let end = books.reduce((a, b) => a + b, 0);
  let result = end;
  while (start <= end) {
    const mid = Math.floor((start + end) / 2);
    if (isPossible(books, students, mid)) {
      result = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  return result;
}

const books = [12, 34, 67, 90];
const students = 2;
console.log(allocateMinimumPages(books, students));
A113
B157
C90
D67
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the maximum pages a student can get while ensuring all books are allocated.
🧠 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 instead of a linear search?
ABecause linear search cannot be used on arrays of numbers.
BBecause binary search sorts the array before allocation.
CBecause binary search always finds the maximum pages directly without checking feasibility.
DBecause binary search efficiently narrows down the minimum maximum pages by checking feasibility in logarithmic time.
Attempts:
2 left
💡 Hint
Consider how the search space is reduced when checking if allocation is possible.
🔧 Debug
advanced
1:30remaining
Identify the Bug in the Allocation Feasibility Check
What error will occur when running this feasibility check function for allocation, and why? function isPossible(books: number[], students: number, maxPages: number): boolean { let requiredStudents = 1; let currentSum = 0; for (const pages of books) { if (pages >= maxPages) return false; if (currentSum + pages > maxPages) { requiredStudents++; currentSum = pages; if (requiredStudents > students) return false; } else { currentSum += pages; } } return true; }
AIt always returns true regardless of input.
BIt throws a runtime error due to uninitialized variables.
CIt returns false incorrectly when a book has pages equal to maxPages, causing wrong allocation.
DIt causes an infinite loop.
Attempts:
2 left
💡 Hint
Check the condition that compares pages with maxPages carefully.
Predict Output
advanced
2:00remaining
Result of Allocation with Multiple Students and Books
What is the output of the following code snippet?
DSA Typescript
const books = [10, 20, 30, 40, 50];
const students = 3;

function isPossible(books: number[], students: number, maxPages: number): boolean {
  let requiredStudents = 1;
  let currentSum = 0;
  for (const pages of books) {
    if (pages > maxPages) return false;
    if (currentSum + pages > maxPages) {
      requiredStudents++;
      currentSum = pages;
      if (requiredStudents > students) return false;
    } else {
      currentSum += pages;
    }
  }
  return true;
}

function allocateMinimumPages(books: number[], students: number): number {
  let start = Math.max(...books);
  let end = books.reduce((a, b) => a + b, 0);
  let result = end;
  while (start <= end) {
    const mid = Math.floor((start + end) / 2);
    if (isPossible(books, students, mid)) {
      result = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  return result;
}

console.log(allocateMinimumPages(books, students));
A100
B60
C90
D70
Attempts:
2 left
💡 Hint
Try dividing books into 3 parts with minimum maximum sum.
🧠 Conceptual
expert
2:30remaining
Why is Binary Search on Answer Space Optimal for Allocate Minimum Pages?
Explain why binary searching on the answer space (range of possible maximum pages) is more efficient than trying all possible allocations directly.
ABecause the answer space is sorted and monotonic, allowing binary search to discard half the possibilities each time, reducing complexity from exponential to logarithmic.
BBecause binary search sorts the books first, making allocation easier.
CBecause trying all allocations directly is faster but uses more memory.
DBecause binary search guarantees the maximum pages will be the sum of all books.
Attempts:
2 left
💡 Hint
Think about how feasibility changes as maximum pages allowed increases or decreases.