Minimum Number of Days to Make m Bouquets

MediumBinary Searchbinary search on answer

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A garden has n flowers in a row. Flower i blooms on day bloomDay[i] and stays bloomed forever after. You want to make m bouquets, and each bouquet needs exactly k **adjacent** bloomed flowers. A flower can belong to at most one bouquet. Return the minimum day number on which it is possible to make all m bouquets. If it can never be done (there are fewer than m * k flowers in total), return -1. Note that m * k can exceed the 32-bit range. Aim for O(n log(max bloomDay)) time.

Examples

Input: bloomDay = [2,9,4,1,7,3], m = 2, k = 2
Output: 7
On day 4 the bloomed flowers are at indices 0, 2, 3, 5, giving only one adjacent pair. On day 7 indices 2 through 5 are all bloomed, giving two pairs.
Input: bloomDay = [5,5,5], m = 2, k = 2
Output: -1
Two bouquets of two flowers need 4 flowers but the garden only has 3.

Constraints

  • 1 <= bloomDay.length <= 10^5
  • 1 <= bloomDay[i] <= 10^9
  • 1 <= m <= 10^6
  • 1 <= k <= n

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search on answer problems