2Brute2Best
🔥0

Minimum Number of Days to Make m Bouquets

Medium·LeetCode #1482 Pro OnlySlight Variation
LeetCode
01

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.

Input

An integer array bloomDay, and integers m and k.

Output

One integer: the minimum number of days to wait, or -1.

Example 1
Input: bloomDay = [1, 10, 3, 10, 2], m = 3, k = 1
Output: 3

Each bouquet needs just 1 flower. On day 3 the flowers blooming on days 1, 3 and 2 are open: 3 bouquets.

Example 2
Input: bloomDay = [1, 10, 3, 10, 2], m = 3, k = 2
Output: -1

3 bouquets of 2 flowers need 6 flowers, but there are only 5.

Example 3
Input: bloomDay = [7, 7, 7, 7, 12, 7, 7], m = 2, k = 3
Output: 12

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.

Constraints
  • • 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.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“minimum number of days”
→ "Minimum X such that it is possible" → binary search on the answer X (here, a day).
“flower stays bloomed”
→ Waiting longer never hurts → the check is monotonic in the day.
“k adjacent flowers”
→ The check counts runs of neighbours, not just open flowers. A closed flower breaks the run.
“return -1 if impossible”
→ Before searching, test `m · k > n`. If enough flowers exist, the last bloom day always works.
03

Approach Complexity Ladder

Brute Force — try every bloom day in orderO(N · D + N log N)O(N)

Sort the distinct bloom days; return the first one where m bouquets can be made

Optimal — binary search on the dayO(N · log(max − min))O(1)

Binary search the day in [min, max]; check by counting runs of k adjacent open flowers

Brute Force Intuition

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:

"Minimum capacity / speed / days such that it is possible." Guess an answer, check feasibility greedily, shrink the range.
Remember this · Binary Search
Trigger
Sorted data, "minimum X such that…", "maximum X such that…", rotated arrays, answers that are monotonic (if X works, X+1 also works).
Think
Is there a yes/no question whose answers look like NNNNYYYY? Then binary search for the first Y.
Key question
What is my check function, and is it monotonic?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.