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
π 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
flowerscounter for consecutive bloomed flowers.When
flowers == k, we can form a bouquet, incrementbouquetscount.Reset
flowers = 0after 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, returnleftas 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++)
β Code (Java)
π Code (Python)
π§ 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