Minimum Days to Make M Bouquets
Given an integer array bloomDay where bloomDay[i] is the day the i-th flower blooms, an integer m representing the number of bouquets needed, and an integer k representing the number of adjacent flowers required per bouquet, return the minimum number of days to make m bouquets. If it is impossible, return -1.
This is a classic binary search on answer problem. The minimum possible day is the minimum bloomDay (at least one flower blooms), and the maximum is the maximum bloomDay (all flowers bloom). Binary search across this range to find the smallest day where at least m bouquets of k adjacent flowers can be formed.
Feasibility is checked by scanning the garden: iterate through flowers, counting consecutive bloomed flowers (those with bloomDay <= current day). When the count reaches k, form a bouquet, reset the counter, and increment the bouquet count. At the end, check if the bouquet count is at least m.
If m * k > total flowers, it is impossible from the start and return -1 immediately. This is a fast-path optimization before any binary search.
The adjacency requirement (k adjacent flowers per bouquet) makes this problem distinct from simple counting. The scan must track consecutive bloomed flowers and reset on gaps. The binary search on answer pattern finds the earliest day satisfying both the minimum bloom condition and the adjacency constraint.
Example Input & Output
Need 6 flowers but only 5 exist.
All four in one bouquet on day 5.
Single bouquet from first flower on day 1.
On day 12, all bloom: [7,7,7] and [7,12,7]...
On day 3, flowers at indices 0,2,4 bloom → 3 bouquets.
Algorithm Flow

Solution Approach
Binary search between min and max bloomDay. Feasibility: scan garden counting consecutive bloomed flowers to form bouquets.
Time O(n log D), Space O(1).
Best Answers
class Solution {
public int solution(int[] bloomDay, int m, int k) {
if (m * k > bloomDay.length) return -1;
int lo = bloomDay[0], hi = bloomDay[0];
for (int d : bloomDay) { if (d < lo) lo = d; if (d > hi) hi = d; }
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (can(bloomDay, mid, m, k)) hi = mid;
else lo = mid + 1;
}
return lo;
}
private boolean can(int[] d, int day, int m, int k) {
int bouquets = 0, adj = 0;
for (int v : d) {
if (v <= day) adj++;
else adj = 0;
if (adj == k) { bouquets++; adj = 0; }
}
return bouquets >= m;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
