Loading problems...
TL;DR: The optimal solution uses Binary Search on the answer (the number of days) combined with a greedy verification step to determine if m bouquets can be formed by a specific day.
In the Minimum Number of Days to Make m Bouquets problem, you are given an array representing the bloom day of flowers in a garden, the number of bouquets required (m), and the number of adjacent flowers needed per bouquet (k). You must determine the minimum number of days to wait until enough flowers have bloomed to form the required bouquets. The constraint is strict: flowers in a single bouquet must be adjacent in the original array. If it is impossible to form the bouquets, return -1.
This is a classic optimization problem found in interviews, often categorized as a "Min-Max" problem solvable via LeetCode 1482 Solution techniques involving binary search.
The naive approach involves checking every possible day starting from the earliest bloom day to the latest bloom day found in the array.
For every single day in the range , we iterate through the entire garden array to see if we can form bouquets using only flowers that have bloomed by day . The first day that satisfies the condition is our answer.
mPseudo-code:
textmin_day = min(bloomDay) max_day = max(bloomDay) for day from min_day to max_day: if canMakeBouquets(day, bloomDay, m, k): return day return -1
Why this fails: The time complexity of this approach is , where is the number of flowers and is the difference between the maximum and minimum bloom days.
The key intuition is moving from searching the array indices to searching the value range (days). Instead of asking "Where is the target?", we ask "Is day feasible?"
Because the feasibility function is monotonic (False, False, ..., True, True), we can binary search for the boundary where the condition switches from False to True.
The Feasibility Check (Condition Function):
To verify if a specific day allows us to make m bouquets:
bloomDay array.k, we form a bouquet. Increment the bouquet count and reset the adjacent flower counter to 0.Visual Description:
Imagine the bloomDay array as a row of lights. On a specific test day, some lights turn on (bloom) and others stay off.
bloomDay: [1, 10, 3, 10, 2], check Day = 3.[ON, OFF, ON, OFF, ON]k.k=1, we find 3 segments.k=2, we find 0 segments because the "OFF" lights break the adjacency.This linear scan takes time. By performing this check inside a binary search over the range of days, we achieve high efficiency.

Let's trace Example 1: bloomDay = [1, 10, 3, 10, 2], m = 3, k = 1.
left = 1, right = 10.mid = 5.right = 5.[1, 5]. mid = 3.right = 3.[1, 3]. mid = 2.left = mid + 1 = 3.left is 3, right is 3. Loop ends.The algorithm relies on the monotonicity of the problem space. Let be a boolean function that returns true if we can make bouquets on day , and false otherwise.
cppclass Solution { public: int minDays(vector<int>& bloomDay, int m, int k) { // Use long long to prevent overflow for m * k long long val = (long long)m * k; if (val > bloomDay.size()) return -1; int left = INT_MAX, right = INT_MIN; for (int day : bloomDay) { left = min(left, day); right = max(right, day); } int ans = -1; while (left <= right) { int mid = left + (right - left) / 2; if (canMake(bloomDay, mid, m, k)) { ans = mid; right = mid - 1; // Try smaller days } else { left = mid + 1; // Need more days } } return ans; } private: bool canMake(const vector<int>& bloomDay, int day, int m, int k) { int bouquets = 0; int adjacent_flowers = 0; for (int bloom : bloomDay) { if (bloom <= day) { adjacent_flowers++; if (adjacent_flowers == k) { bouquets++; adjacent_flowers = 0; // Reset after making a bouquet } } else { adjacent_flowers = 0; // Reset if adjacency is broken } if (bouquets >= m) return true; } return bouquets >= m; } };
javaclass Solution { public int minDays(int[] bloomDay, int m, int k) { // Check feasibility using long to avoid overflow if ((long) m * k > bloomDay.length) { return -1; } int left = Integer.MAX_VALUE; int right = Integer.MIN_VALUE; for (int day : bloomDay) { left = Math.min(left, day); right = Math.max(right, day); } int ans = -1; while (left <= right) { int mid = left + (right - left) / 2; if (canMake(bloomDay, mid, m, k)) { ans = mid; right = mid - 1; // Try to minimize the days } else { left = mid + 1; // Increase days } } return ans; } private boolean canMake(int[] bloomDay, int day, int m, int k) { int bouquets = 0; int adjacentFlowers = 0; for (int bloom : bloomDay) { if (bloom <= day) { adjacentFlowers++; if (adjacentFlowers == k) { bouquets++; adjacentFlowers = 0; } } else { adjacentFlowers = 0; } if (bouquets >= m) return true; } return false; } }
pythonclass Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: if m * k > len(bloomDay): return -1 def can_make(day): bouquets = 0 flowers = 0 for bloom in bloomDay: if bloom <= day: flowers += 1 if flowers == k: bouquets += 1 flowers = 0 else: flowers = 0 if bouquets >= m: return True return False left, right = min(bloomDay), max(bloomDay) ans = -1 while left <= right: mid = (left + right) // 2 if can_make(mid): ans = mid right = mid - 1 else: left = mid + 1 return ans
bloomDay).The Binary Search on Answer pattern is highly reusable. It applies whenever you are asked to find the minimum or maximum value that satisfies a condition, and that condition is monotonic.
Similar problems using this pattern:
k to finish in h hours)D days)In all these cases, the logic is identical: define a search range, pick a mid-point, check feasibility with a greedy function, and adjust bounds.
Why do I get a Wrong Answer on large inputs?
You might be encountering integer overflow. The check m * k can exceed the 32-bit integer limit (). In C++ and Java, you must cast one of the operands to long or long long before multiplication (e.g., (long)m * k).
Why does my solution TLE even with Binary Search?
Ensure your canMake function is strictly . If you are using nested loops or sorting inside the check function, the complexity increases. Also, verify your binary search bounds. If you set right to a fixed constant like instead of max(bloomDay), it is still valid but slightly less efficient.
Why does the greedy approach work? Shouldn't I skip some flowers?
No. This is a common conceptual gap. Because we need adjacent flowers, skipping a valid bloomed flower at index i to start a bouquet at i+1 never helps. It only reduces the contiguous segment available. Greedily completing a bouquet as soon as count reaches k is always optimal.
Why is my loop infinite?
This usually happens due to incorrect binary search boundary updates. If you use left < right loop condition, ensure right = mid (not mid - 1) when the condition is met. If you use left <= right, ensure right = mid - 1 and left = mid + 1. The provided solutions use the left <= right template which is generally less error-prone for beginners.