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`.

Be the first one to answer
Add answer anonymously...
TCS Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter