24. Minimum Days to Make M Bouquets
β GFG solution to the Minimum Days to Make M Bouquets problem: find minimum days required to create m bouquets using binary search optimization technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You have a row of flowers, where each flower blooms after a specific day. The array arr[]
represents the blooming schedule: arr[i]
is the day the flower at position i
will bloom. To create a bouquet, you need to collect k adjacent bloomed flowers. Each flower can only be used in one bouquet.
Your goal is to find the minimum number of days required to make exactly m bouquets. If it is not possible to make m bouquets with the given arrangement, return -1.
π Examples
Example 1
Input: m = 3, k = 2, arr[] = [3, 4, 2, 7, 13, 8, 5]
Output: 8
Explanation: We need 3 bouquets and each bouquet should have 2 flowers.
After day 8: [x, x, x, x, _, x, x], we can make first bouquet from the first 2 flowers,
second bouquet from the next 2 flowers and the third bouquet from the last 2 flowers.
Example 2
Input: m = 2, k = 3, arr[] = [5, 5, 5, 5, 10, 5, 5]
Output: 10
Explanation: We need 2 bouquets and each bouquet should have 3 flowers,
After day 5: [x, x, x, x, _, x, x], we can make one bouquet of the first three flowers
that bloomed, but cannot make another bouquet. After day 10: [x, x, x, x, x, x, x],
Now we can make two bouquets, taking 3 adjacent flowers in one bouquet.
Example 3
Input: m = 3, k = 2, arr[] = [1, 10, 3, 10, 2]
Output: -1
Explanation: As 3 bouquets each having 2 flowers are needed, that means we need 6 flowers.
But there are only 5 flowers so it is impossible to get the needed bouquets therefore -1 will be returned.
π Constraints
$1 \le k \le \text{arr.size()} \le 10^5$
$1 \le m \le 10^5$
$1 \le \text{arr}[i] \le 10^9$
β
My Approach
The optimal approach uses Binary Search on the answer combined with a Greedy Algorithm to check feasibility:
Binary Search + Greedy Validation
Check Impossibility:
If
k Γ m > arr.size()
, it's impossible to make m bouquets, return -1.
Set Search Range:
left
= minimum bloom day in array (earliest possible answer)right
= maximum bloom day in array (latest possible answer)
Binary Search on Days:
For each mid-day, check if we can make m bouquets by that day.
Use greedy approach: scan array left to right, count consecutive bloomed flowers.
Greedy Bouquet Formation:
Maintain
flowers
counter for consecutive bloomed flowers.When
flowers == k
, we can form a bouquet, incrementbouquets
count.Reset
flowers = 0
after forming a bouquet or encountering unbloomed flower.
Update Search Range:
If
bouquets >= m
: answer exists at mid or earlier, setright = mid
.Otherwise: need more days, set
left = mid + 1
.
Return Result:
Continue until
left >= right
, returnleft
as minimum days needed.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n Γ log(max(arr) - min(arr))), where n is the size of the array. Binary search runs for log(range) iterations, and each iteration requires O(n) time to validate bouquet formation.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional variables for tracking flowers, bouquets, and search boundaries.
π§βπ» Code (C++)
class Solution {
public:
int minDaysBloom(vector<int>& arr, int k, int m) {
if ((long long)k * m > arr.size()) return -1;
int l = *min_element(arr.begin(), arr.end());
int r = *max_element(arr.begin(), arr.end());
while (l < r) {
int mid = l + (r - l) / 2;
int flowers = 0, bouquets = 0;
for (int bloom : arr) {
if (bloom <= mid) {
if (++flowers == k) {
bouquets++;
flowers = 0;
}
} else flowers = 0;
}
if (bouquets >= m) r = mid;
else l = mid + 1;
}
return l;
}
};
β Code (Java)
class Solution {
public int minDaysBloom(int[] arr, int k, int m) {
if ((long) k * m > arr.length) return -1;
int l = Arrays.stream(arr).min().getAsInt();
int r = Arrays.stream(arr).max().getAsInt();
while (l < r) {
int mid = l + (r - l) / 2;
int flowers = 0, bouquets = 0;
for (int bloom : arr) {
if (bloom <= mid) {
if (++flowers == k) {
bouquets++;
flowers = 0;
}
} else flowers = 0;
}
if (bouquets >= m) r = mid;
else l = mid + 1;
}
return l;
}
}
π Code (Python)
class Solution:
def minDaysBloom(self, arr, k, m):
if k * m > len(arr): return -1
l, r = min(arr), max(arr)
while l < r:
mid = (l + r) // 2
flowers = bouquets = 0
for bloom in arr:
if bloom <= mid:
flowers += 1
if flowers == k:
bouquets += 1
flowers = 0
else:
flowers = 0
if bouquets >= m:
r = mid
else:
l = mid + 1
return l
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated