Minimum Number of Days to Make m Bouquets

medium

You look after a flower garden — n flowers planted in a single row. The i-th flower blooms on day bloomDay[i], stays open forever after, and can be cut for exactly one bouquet.

You have an order for m bouquets. Each bouquet needs exactly k adjacent flowers — side-by-side in the row, no gaps, and no flower shared between bouquets.

Return the minimum number of days you must wait until the garden can supply all m bouquets. If it is impossible no matter how long you wait, return -1.

Hints

Waiting never hurts: if the garden can fill the order by day d, it can certainly fill it by day d + 1. What shape does that give the sequence of yes/no answers across the calendar?
Freeze a single day d and ask a much simpler question: how many bouquets can be cut using only flowers with bloomDay[i] <= d? One left-to-right pass suffices — grow a streak of adjacent bloomed flowers, and every time it reaches k, cut a bouquet and reset the streak.
The answers over days form NO NO ... NO YES YES ... YES — binary search [min(bloomDay), max(bloomDay)] for the first YES. Handle the impossible case up front: if m * k > n, return -1 (compute the product in 64-bit).

Common doubts

Nothing changes between bloom days — the set of open flowers only grows on a day equal to some bloomDay[i]. Feasibility can therefore only flip from no to yes on a bloom day, and binary search over the full range still converges exactly onto that first flip.
Within a run of s consecutive bloomed flowers you can fit at most s / k (floor) disjoint bouquets, and the greedy reset achieves exactly that. Delaying a cut can only waste flowers at the front of the run, never gain any.
No on both counts — each flower is used in exactly one bouquet, and the k flowers must be adjacent, so an unbloomed flower breaks the streak and separates runs.
It is the only impossible case — with enough waiting every flower blooms, so any garden with at least m * k flowers eventually works. Beware the product: it can reach 10^11, which overflows 32-bit integers in C++.

Interview follow-ups

Adjacency was the whole difficulty. Without it you just need the day by which m * k flowers have bloomed — the (m * k)-th smallest bloom day, found by sorting in O(n log n) or quickselect in average O(n).
Any minimize-the-answer problem with a monotone feasibility check: Koko Eating Bananas (search the eating speed), Find the Smallest Divisor Given a Threshold (search the divisor), Capacity to Ship Packages Within D Days (search the capacity), Split Array Largest Sum (search the max subarray sum). Only the O(n) checker changes.
Yes — process flowers in increasing bloom-day order and merge adjacent bloomed segments (union-find or a length map), tracking how many bouquets the merged segments yield; the answer is the bloom day whose merge first reaches m bouquets. It runs in O(n log n) and makes a great discussion of trade-offs versus the binary-search approach.

Fun facts

  • With bloom days up to 10^9, the binary search asks at most ~30 yes/no questions — you interrogate a billion-day calendar for the cost of scanning the garden thirty times.
  • The skeleton here — monotone feasibility plus binary search on the value space — is one of the most reused patterns in interviews: the same six lines solve Koko Eating Bananas and Capacity to Ship Packages Within D Days with a different one-pass check swapped in.

Asked at

GoogleAmazonMicrosoftFlipkart
Frequently Sometimes Occasionally
Example 1
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Each bouquet needs just 1 flower, so we need any 3 bloomed flowers. Writing x for bloomed and _ for not yet:
After day 1: [x, _, _, _, _] — 1 bouquet.
After day 2: [x, _, _, _, x] — 2 bouquets.
After day 3: [x, _, x, _, x] — 3 bouquets. Day 3 is the first day the order can be filled.
Example 2
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
3 bouquets of 2 flowers each need 6 flowers, but the garden only has 5. No amount of waiting helps — return -1.
Example 3
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
After day 7 the garden looks like [x, x, x, x, _, x, x]. The first four flowers give one bouquet, but the remaining bloomed flowers are split by the unbloomed one — no second bouquet of 3 adjacent flowers. After day 12 every flower is open and both bouquets are easy, so the answer is 12.
Constraints

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

Solve this problem →