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.
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.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.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.k flowers must be adjacent, so an unbloomed flower breaks the streak and separates runs.m * k flowers eventually works. Beware the product: it can reach 10^11, which overflows 32-bit integers in C++.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.
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.
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.
- bloomDay.length == n - 1 <= n <= 10^5 - 1 <= bloomDay[i] <= 10^9 - 1 <= m <= 10^6 - 1 <= k <= n
Most search problems ask you to search a collection. This one asks you to search time itself: the array never changes — only the calendar does. The arc: see why waiting can only help, turn a fixed day into a one-pass counting question, then binary search the calendar for the first day that says yes.
In plain English: flowers bloom on given days, a bouquet is k adjacent bloomed flowers, every flower is used at most once — find the earliest day on which m disjoint bouquets exist.
Formally: find the minimum d such that the positions i with bloomDay[i] <= d, read left to right, contain at least m disjoint runs of k consecutive bloomed flowers. If m * k > n, no such d exists — return -1.
Worked example — bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
after day 7 : x x x x _ x x runs: 4 and 2
[--1--]? 4 // 3 = 1 bouquet, 2 // 3 = 0 → only 1, not enough
after day 12: x x x x x x x run: 7
[--1--][--2--] 7 // 3 = 2 bouquets ✓
answer: 12
A senior candidate pins down the rules of the garden before writing a line of code.
“Must the k flowers in one bouquet be adjacent in the row?”
Yes — this is the heart of the problem. Without adjacency the answer is simply the (m·k)-th smallest bloom day.
“Can a flower belong to two bouquets?”
No — each flower is cut exactly once, so bouquets are disjoint runs.
“Can m times k exceed the number of flowers?”
Yes, and then the answer is -1 — worth handling up front before any searching.
“Are bloom days always positive integers?”
Yes, at least 1 — so the search window min(bloomDay) to max(bloomDay) is well-defined.
“How large can bloomDay[i] get?”
Up to 10^9 — simulating the garden day by day is hopeless, which is a strong hint to search days cleverly instead.
“How large is n?”
Up to 10^5 — an O(n) check per probed day is fine; an O(n) check per distinct day is not.
Before I code, let me confirm the bouquet rules: the k flowers must be adjacent, and each flower is used in at most one bouquet.
If m times k exceeds n, it is impossible and I should return -1 — I will check that first, in 64-bit since the product can reach 10^11.
Bloom days go up to a billion, so instead of simulating days I will binary search the day, using a linear feasibility check.
If the order can be filled by day d, every flower open on day d is still open on day d + 1, so it can be filled then too. Across the calendar the answers form a single flip:
day: 1 2 3 4 5 6 7 ...
can make? NO NO NO YES YES YES YES ...
^ the answer is the first YESOne flip means binary search applies — not on the array, but on the day.
Freeze a day d. A flower is open iff bloomDay[i] <= d. Scan left to right keeping a streak of consecutive open flowers; the moment it reaches k, cut a bouquet and reset the streak to 0; an unbloomed flower also resets it. A run of s open flowers yields exactly s // k bouquets, and cutting as early as possible never hurts — so this greedy count is the maximum. The check is O(n).
The answer lies in [min(bloomDay), max(bloomDay)] — before the first bloom nothing is possible, and by the last bloom everything that will ever be possible already is. Binary search that window for the first YES: about log2(10^9) ≈ 30 probes, each an O(n) scan. Handle m * k > n up front with -1.
Let d be the number of distinct bloom days and D the largest bloom day.
| Brute force | Optimal | |
|---|---|---|
| Idea | try each distinct bloom day in order | binary search the day, greedy check |
| Time | O(n · d) | O(n log D) |
| Space | O(d) | O(1) |
Full, runnable code for both lives in the Approaches selector below.
Key takeaway
When the answer is a number and you can cheaply check would this value work? — with the answers monotone across the range — binary search the answer space instead of the input. The exact same skeleton solves Koko Eating Bananas, Find the Smallest Divisor Given a Threshold, and Capacity to Ship Packages Within D Days; only the feasibility check changes.
if m * k > n: return -1
lo, hi = min(bloomDay), max(bloomDay)
while lo < hi:
mid = (lo + hi) / 2
if canMake(mid): hi = mid # mid works — try to wait less
else: lo = mid + 1 # too early — must wait longer
return lo # the first day the calendar says yes