Problem · Binary Search

Find Minimum Pages Per Day

Learn this problem
MediumAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

A student is preparing for a test from Amazon Academy for a scholarship.

The student is required to completely read n chapters (which is the length of the pages array) for the test, where the ith chapter has pages[i] number of pages. The chapters are read in increasing order of the index. Each day the student can either read till the end of a chapter or at most x pages, whichever is minimum. The number of pages remaining to read decreases by x in the latter case.

The test will be given in days number of days from now. Find the minimum number of pages, x, which the student should read each day to finish all pages of all chapters within days number of days. If it is not possible to finish these chapters in days number of days, return -1.

Note: In one day, the student cannot read pages of more than one chapter. Thus, if a chapter finishes, the next chapter starts only on the next day even if the number of pages read is less than x.

Function

findMinimumPagesPerDay(pages: int[], days: int) → int

Complete the function findMinimumPagesPerDay in the editor.

findMinimumPagesPerDay has the following parameters:

  • int pages[n]: an array of integers representing the number of pages in each chapter
  • int days: the number of days to finish reading

Returns

int: the minimum number of pages to read each day, or -1 if it is not possible.

Examples

Example 1

pages = [5, 3, 4]days = 4return = 4
Day 1: The student reads A pages of the first chapter - pages remaining = [1, 3, 4] Day 2: The student can only read till the end of the first chapter - pages remaining = [0, 3, 4] Day 3: The student can only read till the end of the chapter x = 4 pages, since 3<4, the student reads till the end of the chapter 2- pages remaining = [0, 0, 4] Day 4: The student reads all the 4 pages of the last chapter - pages remaining = [0, 0, 0]

Example 2

pages = [2, 3, 4, 5]days = 5return = 4
If x = 4, the student would read the 1st, 2nd and 3rd chapters in 1 day each, and the 4th chapter in 2 days. The total num of days taken = 1 + 1 + 1 + 2 = 5, which is within the num of allowed days. If x is less than 4, the chapters cannot be finished within 5 days.

Example 3

pages = [2, 3, 4]days = 4return = 3
Example 3 illustration
Thus, in 4 days, the student can read all pages of all chapters and finish. If x is less than 3, it is impossible to read all chapters in 4 days. Thus, the minimum num of pages read each day is 3.

Constraints

  • n is the number of chapters, where n equals the length of pages and n >= 1.
  • pages[i] >= 1 for every chapter i.
  • days >= 1.
  • Each chapter requires at least one day to read (the next chapter starts only on a new day), so if days < n the answer is -1.
  • The answer x satisfies 1 <= x <= max(pages); it is the smallest x such that the total days needed (summing ceil(pages[i] / x) over all chapters) is at most days.

More Amazon problems

drafts saved locally
public int findMinimumPagesPerDay(int[] pages, int days) {
  // write your code here
}
pages[5, 3, 4]
days4
expected4
checking account