Binary TreesBinary Trees36

  1. 1Preorder Traversal of a Binary Tree using Recursion
  2. 2Preorder Traversal of a Binary Tree using Iteration
  3. 3Postorder Traversal of a Binary Tree Using Recursion
  4. 4Postorder Traversal of a Binary Tree using Iteration
  5. 5Level Order Traversal of a Binary Tree using Recursion
  6. 6Level Order Traversal of a Binary Tree using Iteration
  7. 7Reverse Level Order Traversal of a Binary Tree using Iteration
  8. 8Reverse Level Order Traversal of a Binary Tree using Recursion
  9. 9Find Height of a Binary Tree
  10. 10Find Diameter of a Binary Tree
  11. 11Find Mirror of a Binary Tree - Todo
  12. 12Inorder Traversal of a Binary Tree using Recursion
  13. 13Inorder Traversal of a Binary Tree using Iteration
  14. 14Left View of a Binary Tree
  15. 15Right View of a Binary Tree
  16. 16Top View of a Binary Tree
  17. 17Bottom View of a Binary Tree
  18. 18Zigzag Traversal of a Binary Tree
  19. 19Check if a Binary Tree is Balanced
  20. 20Diagonal Traversal of a Binary Tree
  21. 21Boundary Traversal of a Binary Tree
  22. 22Construct a Binary Tree from a String with Bracket Representation
  23. 23Convert a Binary Tree into a Doubly Linked List
  24. 24Convert a Binary Tree into a Sum Tree
  25. 25Find Minimum Swaps Required to Convert a Binary Tree into a BST
  26. 26Check if a Binary Tree is a Sum Tree
  27. 27Check if All Leaf Nodes are at the Same Level in a Binary Tree
  28. 28Lowest Common Ancestor (LCA) in a Binary Tree
  29. 29Solve the Tree Isomorphism Problem
  30. 30Check if a Binary Tree Contains Duplicate Subtrees of Size 2 or More
  31. 31Check if Two Binary Trees are Mirror Images
  32. 32Calculate the Sum of Nodes on the Longest Path from Root to Leaf in a Binary Tree
  33. 33Print All Paths in a Binary Tree with a Given Sum
  34. 34Find the Distance Between Two Nodes in a Binary Tree
  35. 35Find the kth Ancestor of a Node in a Binary Tree
  36. 36Find All Duplicate Subtrees in a Binary Tree

Minimum Days to Make M Bouquets
Binary Search Optimal Solution



Problem Statement

You are given an array bloomDay[] where bloomDay[i] represents the day on which the i-th flower will bloom. Your task is to determine the minimum number of days required to make m bouquets, where each bouquet requires k adjacent bloomed flowers.

If it's not possible to make m bouquets, return -1.

Examples

Bloom Day ArraymkOutputDescription
[1, 10, 3, 10, 2]313On day 3, flowers at indices 0, 2, and 4 are bloomed. 3 bouquets possible.
[1, 10, 3, 10, 2]32-1Not enough adjacent flowers to form 3 bouquets of 2 flowers.
[7, 7, 7, 7, 12, 7, 7]2312Day 12 is the earliest when two bouquets of 3 adjacent flowers can be made.
[1000000000, 1000000000]111000000000Only one flower needed, take the earliest available.
[1, 10, 2, 9, 3, 8]229Day 9 gives two pairs of adjacent bloomed flowers: (2,3) and (4,5)
[1]111One flower, one bouquet needed — return 1.
[1]21-1Only one flower, but two bouquets required — not possible.
[]11-1Empty array — can't make any bouquets.

Solution

This problem is about figuring out the minimum number of days required to make m bouquets, where each bouquet is formed from k adjacent flowers that have bloomed.

Understanding the Setup

We are given a list of bloom days for each flower. A flower can only be used to form a bouquet on or after the day it blooms. For every bouquet, we need exactly k adjacent flowers. If they're not next to each other, we cannot use them in the same bouquet.

How to Think About It

Imagine checking a particular day — let's say day 6. You can only use flowers that bloom on or before day 6. Now go through the array, and every time you see k or more adjacent bloomed flowers, you make one bouquet. Count how many bouquets you can make this way. If you can make at least m bouquets, then day 6 is a valid answer.

Our goal is to find the minimum such day. So, instead of checking each day one by one (which would be slow), we use binary search between the smallest and largest bloom days. At each step, we check if it’s possible to make enough bouquets by that day. If yes, we try an earlier day (move left); if not, we try a later day (move right).

Special Cases to Consider

  • Not Enough Flowers: If m × k is greater than the total number of flowers, it's impossible to make that many bouquets. We return -1.
  • All Flowers Bloom Late: If flowers bloom very late (e.g., all values are huge), the answer may be a large number, but binary search helps us get there quickly.
  • Empty Array: If the input array is empty, no bouquets can be made. We return -1.
  • Edge Size Matching: If the total number of flowers is exactly m × k, then we must use every flower and they must be positioned in a way that allows adjacency.

This approach gives us a clean and efficient solution with time complexity O(n × log(max bloom day)), where n is the number of flowers.

Algorithm Steps

  1. Set low = minimum bloom day, high = maximum bloom day.
  2. Perform binary search while low ≤ high.
  3. Set mid = (low + high) / 2.
  4. Check if it’s possible to make m bouquets in mid days using a helper function:
    • Traverse bloom days. Count consecutive bloomed roses (i.e., arr[i] ≤ mid).
    • Every time you collect k adjacent, increment bouquet count and reset count.
    • If bouquet count ≥ m, return true.
  5. If possible, store mid as answer and move high = mid - 1.
  6. Else move low = mid + 1.

Code

Java
Python
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
public class Bouquets {
  public static boolean canMakeBouquets(int[] bloomDay, int m, int k, int day) {
    int bouquets = 0, flowers = 0;
    for (int b : bloomDay) {
      if (b <= day) {
        flowers++;
        if (flowers == k) {
          bouquets++;
          flowers = 0;
        }
      } else {
        flowers = 0;  // Reset if bloom not ready
      }
    }
    return bouquets >= m;
  }

  public static int minDays(int[] bloomDay, int m, int k) {
    if ((long)m * k > bloomDay.length) return -1;
    int low = Integer.MAX_VALUE, high = Integer.MIN_VALUE;
    for (int day : bloomDay) {
      low = Math.min(low, day);
      high = Math.max(high, day);
    }
    int ans = -1;
    while (low <= high) {
      int mid = low + (high - low) / 2;
      if (canMakeBouquets(bloomDay, m, k, mid)) {
        ans = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    return ans;
  }

  public static void main(String[] args) {
    int[] bloom = {1, 10, 3, 10, 2};
    int m = 3, k = 1;
    System.out.println("Minimum Days: " + minDays(bloom, m, k));
  }
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)In the best case, the first binary search guess gives a valid answer after a single bouquet check.
Average CaseO(n log(max_day - min_day))Binary search runs in log(max_day - min_day) steps and each step checks the full array to count bouquets.
Average CaseO(n log(max_day - min_day))Binary search iterates across the entire range of possible days, checking all n roses each time.

Space Complexity

O(1)

Explanation: Only a few variables are used; no extra space is needed apart from input.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M