0
0
DSA Typescriptprogramming~30 mins

Allocate Minimum Pages Binary Search on Answer in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Allocate Minimum Pages using Binary Search on Answer
📖 Scenario: You are managing a library where books are arranged in a row. Each book has a certain number of pages. You want to allocate these books to a fixed number of students such that each student gets a continuous sequence of books. Your goal is to minimize the maximum number of pages assigned to any student.This is a common problem in resource allocation where fairness and balance are important.
🎯 Goal: Build a TypeScript program that uses binary search on the answer to find the minimum possible maximum pages allocated to a student.You will create the data structure for books, set the number of students, implement the binary search logic, and finally print the minimum maximum pages.
📋 What You'll Learn
Create an array called books with the exact values [12, 34, 67, 90]
Create a variable called students and set it to 2
Write a function called isPossible that takes mid as input and returns true if allocation is possible with max pages <= mid, else false
Use binary search between the max pages of a single book and the sum of all pages to find the minimum max pages
Print the final minimum maximum pages using console.log
💡 Why This Matters
🌍 Real World
This problem models fair resource allocation such as dividing tasks, workloads, or resources among people or machines to balance load.
💼 Career
Understanding binary search on answer is useful in software engineering roles involving optimization, scheduling, and system design.
Progress0 / 4 steps
1
Create the books array
Create an array called books with the exact values [12, 34, 67, 90]
DSA Typescript
Hint

Use const books: number[] = [12, 34, 67, 90]; to create the array.

2
Set the number of students
Create a variable called students and set it to 2
DSA Typescript
Hint

Use const students: number = 2; to set the number of students.

3
Implement the isPossible function
Write a function called isPossible that takes a number mid and returns true if it is possible to allocate books to students such that no student reads more than mid pages, otherwise returns false
DSA Typescript
Hint

Check if each book can be allocated without exceeding mid pages per student. Increase student count when needed.

4
Use binary search to find minimum maximum pages and print result
Use binary search between maxPages (maximum pages in a single book) and totalPages (sum of all pages) to find the minimum maximum pages. Print the result using console.log
DSA Typescript
Hint

Use binary search between the max single book pages and total pages. Update the result when allocation is possible.