Minimum Number of Days to Make m Bouquets - Problem

You are given an integer array bloomDay, an integer m and an integer k.

You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

Input & Output

Example 1 — Basic Case
$ Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
💡 Note: By day 3, flowers at indices 0, 2, and 4 have bloomed (bloom days 1, 3, 2). Each bouquet needs k=1 adjacent flower, so we can make 3 bouquets from positions [0], [2], [4].
Example 2 — Adjacent Required
$ Input: bloomDay = [1,10,3,10,2], m = 2, k = 2
Output: -1
💡 Note: We need 2 bouquets with k=2 adjacent flowers each (total 4 flowers). But we only have 5 flowers total, and they never form 2 groups of 2 adjacent bloomed flowers simultaneously.
Example 3 — Later Day Needed
$ Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
💡 Note: By day 7, all flowers except index 4 have bloomed. We need 2 groups of 3 adjacent flowers. Only by day 12 when all flowers bloom can we make groups [0,1,2] and [4,5,6].

Constraints

  • 1 ≤ bloomDay.length ≤ 105
  • 1 ≤ bloomDay[i] ≤ 109
  • 1 ≤ m, k ≤ bloomDay.length

Visualization

Tap to expand
Minimum Number of Days to Make m Bouquets INPUT bloomDay Array (Garden) 0 1 2 3 4 1 10 3 10 2 m = 3 k = 1 m = bouquets needed k = adjacent flowers per bouquet Need: 3 x 1 = 3 flowers Have: 5 flowers Possible: OK ALGORITHM STEPS 1 Binary Search Setup left=1, right=10 (min/max) 2 Try mid = 5 Count bouquets at day 5 3 Check Feasibility If bouquets >= m, go left 4 Converge to Answer Find minimum valid day Binary Search Trace Day 5: [1,10,3,10,2] Bloomed: [Y,N,Y,N,Y]=3 OK Day 2: [1,10,3,10,2] Bloomed: [Y,N,N,N,Y]=2 NO Day 3: [1,10,3,10,2] Bloomed: [Y,N,Y,N,Y]=3 OK FINAL RESULT Garden State at Day 3 1 10 3 10 2 Bloomed Not yet 3 Bouquets Formed (k=1 each) B1 B2 B3 Output: 3 Key Insight: Binary search on the answer! If we can make m bouquets on day D, we can also make them on any day > D. This monotonic property allows binary search on days [min(bloomDay), max(bloomDay)]. Time: O(n * log(max-min)) where n = array length. For each day, scan array to count adjacent bloomed flowers. TutorialsPoint - Minimum Number of Days to Make m Bouquets | Binary Search Approach
Asked in
Facebook 35 Google 28 Amazon 22
89.3K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen