AlgoMaster Logo

Minimum Number of Days to Make m Bouquets

mediumUpdated September 21, 2026

Understanding the Problem

We have a garden of n flowers, and each flower blooms on a specific day. We need to make m bouquets, and each bouquet requires exactly k adjacent (consecutive) flowers that have already bloomed. A flower can only be used in one bouquet.

This problem has a monotonic property. If we can make m bouquets by day d, we can also make them by day d + 1, because more flowers will have bloomed by then and no bloomed flower ever wilts. Conversely, if we cannot make m bouquets by day d, we cannot make them by any earlier day either. This "if feasible at day d, then feasible at day d+1" property means binary search on the answer applies.

The second piece is the feasibility check. Given a specific day d, we walk through the array left to right, counting consecutive bloomed flowers. Every time the consecutive count reaches k, that is one bouquet, and we reset the counter. This greedy scan tells us how many bouquets we can make on day d.

Key Constraints:

  • 1 <= n <= 10^5 --> The feasibility check (scanning the array) must be O(n) or better per call.
  • 1 <= bloomDay[i] <= 10^9 --> The search space for the answer is up to 10^9, so we cannot try every day linearly. Binary search over days gives O(log(10^9)) = ~30 iterations.
  • 1 <= m <= 10^6 --> We must check early whether m * k > n. If so, the answer is immediately -1.

Approach 1: Brute Force (Try Every Bloom Day)

Intuition

Collect all unique bloom days, sort them, and try each one as a candidate answer. For each candidate day, scan the array to count how many bouquets we can make. The first day where we can make at least m bouquets is the answer.

We only check unique bloom days because the number of bloomed flowers changes only on days when a new flower blooms. Checking day 5 and day 6 gives the same result if no flower blooms on day 6, so the distinct values in bloomDay cover every meaningful candidate.

The feasibility check walks left to right, keeping a running count of consecutive bloomed flowers. Every time the count reaches k, increment the bouquet count and reset.

Algorithm

  1. If m * k > n, return -1 immediately (not enough flowers).
  2. Collect all unique bloom days and sort them in ascending order.
  3. For each unique day d (smallest to largest):
    • Scan the bloomDay array left to right.
    • Maintain a counter of consecutive flowers where bloomDay[i] <= d.
    • Every time this counter reaches k, increment the bouquet count and reset the counter to 0.
    • If the bouquet count reaches m, return d.
  4. If no day works, return -1.

Visualization and Code

Loading animation...

When most bloom days are distinct, this checks close to n candidates and degrades to O(n^2). Because the feasibility function is monotonic (once true, it stays true for all larger days), binary search can find the boundary day without checking every candidate.

Approach 2: Binary Search on Answer

Intuition

If we can make m bouquets on day d, we can also make them on day d + 1, d + 2, and every day after that. More flowers bloom as days pass, never fewer. So the feasibility function over days looks like false, false, ..., false, true, true, ..., true, and we want the first true.

That structure is what binary search exploits. Instead of searching the array for an element, we search the space of possible answers (days from min(bloomDay) through max(bloomDay)) for the smallest day that satisfies the condition.

The search space is bounded: the answer must be at least min(bloomDay) (can't make any bouquet before the first flower blooms) and at most max(bloomDay) (by then all flowers have bloomed). We binary search within this range, and at each midpoint we run our greedy feasibility check.

Algorithm

  1. If m * k > n, return -1 immediately (not enough flowers even if all bloom).
  2. Set low = min(bloomDay) and high = max(bloomDay).
  3. While low < high:
    • Compute mid = low + (high - low) / 2.
    • Run the feasibility check: scan the array, count consecutive flowers where bloomDay[i] <= mid, and count bouquets.
    • If we can make at least m bouquets, set high = mid (this day works, but maybe an earlier day also works).
    • Otherwise, set low = mid + 1 (need more days).
  4. Return low.

Visualization and Code

Loading animation...