Minimum Number of Days to Make m Bouquets
Problem Statement
A garden has n flowers in a row. Flower i blooms on day bloomDay[i] and stays open after that.
You want to make m bouquets. Each bouquet needs exactly k flowers that are next to each other in the row and have all bloomed. A flower can be used in only one bouquet.
Return the earliest day on which you can make m bouquets. If it is never possible, return -1.
An integer array bloomDay, and integers m and k.
One integer: the minimum number of days to wait, or -1.
Each bouquet needs just 1 flower. On day 3 the flowers blooming on days 1, 3 and 2 are open: 3 bouquets.
3 bouquets of 2 flowers need 6 flowers, but there are only 5.
On day 7, the open flowers are [x x x x _ x x]. Only the first 4 are adjacent, which gives just 1 bouquet. On day 12 all 7 flowers are open, so [7, 7, 7] and [7, 12, 7] make 2 bouquets.
- • 1 ≤ n ≤ 10^5
- • 1 ≤ bloomDay[i] ≤ 10^9
- • 1 ≤ m ≤ 10^6
- • 1 ≤ k ≤ n
- • m · k can reach 10^11 — compute it in 64 bits.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Sort the distinct bloom days; return the first one where m bouquets can be made
Binary search the day in [min, max]; check by counting runs of k adjacent open flowers
Nothing new blooms between two bloom days, so the answer is always one of the values in bloomDay. Sort the distinct values and try them from smallest to largest. For each day, count bouquets with a left-to-right scan. Return the first day that gives at least m.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.