Allocate Books Problem Statement

Given an array of integers arr, where arr[i] represents the number of pages in the i-th book, and an integer m representing the number of students, allocate all the books in such a way that:

  • Each student gets at least one book.
  • Each book should be allocated to a student.
  • Book allocation should be contiguous.

The goal is to allocate the books such that the maximum number of pages assigned to a student is minimized.

Input:

The first line contains an integer `T`, the number of test cases.
The next 2*T lines represent the test cases.
The first line of each test case contains two space-separated integers `n` and `m`, representing the number of books and students respectively.
The second line contains `n` space-separated integers denoting the number of pages in each book.

Output:

For each test case, return the minimum possible number of pages that can be allocated to a student such that the maximum number of pages assigned is minimized.

Example:

Consider `n=4` books and `m=2` students. Suppose,
arr = {10, 20, 30, 40}

All possible allocations are:

  • 10 | 20, 30, 40 - maximum `max(10, 90) = 90`
  • 10, 20 | 30, 40 - maximum `max(30, 70) = 70`
  • 10, 20, 30 | 40 - maximum `max(60, 40) = 60`

The minimum of these maximums is `min(90, 70, 60) = 60`, hence the output should be 60.

Constraints:

  • 1 <= T <= 50
  • 2 <= M <= N <= 10^3
  • 1 <= arr[i] <= 10^9
  • Sum of all arr[i] does not exceed 10^9.
  • The time limit is 1 second.
Note:

Return the minimum pages allocated in each test case and if it's not possible to allocate books to all students, return `-1`.

AnswerBot
4mo

Allocate books to students in a way that minimizes the maximum number of pages assigned to a student.

  • Iterate through possible allocations and calculate the maximum pages assigned to a student.

  • Find the...read more

Help your peers!
Select
Add answer anonymously...

Top Front end Developer Interview Questions Asked at TCS

Q. What is Redux, and what are the differences between Redux and Context?
Q. What is the difference between visibility: hidden and display: none in CSS?
Q. What are props in React?
Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits