MediumArrayBinary SearchUpdated Sep 2026

1482. Minimum Number of Days to Make m Bouquets

Asked at Flipkart

Difficulty

Medium

Time

O(n log D)

Space

O(1)

Pattern

Array

Companies

1

Problem

You have a garden with n flowers in a row. Flower i opens on day bloomDay[i]. Once a flower is open, it can be picked and used in exactly one bouquet.

You want to build m bouquets. Each bouquet needs k flowers, and those k flowers must be next to each other in the garden. Return the smallest day d such that by day d you can build all m bouquets. If it can never be done, return -1.

A flower is usable on day d if bloomDay[i] <= d. Flowers that are not yet open break any run of adjacent flowers, so they cannot be part of a bouquet.

Which flowers are open on day 3?

bloomDay110310201234On day 3, indices 0, 2 and 4 have bloomDay <= 3, so 3flowers are open.day 2openclosedclosedclosedopen01234On day 2 only 2 flowers are open, so we cannot reach 3bouquets yet.
With m = 3 and k = 1 we only need 3 open flowers, and day 3 is the first day that gives us that many.

Constraints

  • ●n == bloomDay.length
  • ●1 <= n <= 10^5
  • ●1 <= bloomDay[i] <= 10^9
  • ●1 <= m <= 10^6
  • ●1 <= k <= n

Examples

Example 1
InputbloomDay = [1,10,3,10,2], m = 3, k = 1
Output3
WhyEach bouquet needs just 1 flower, so we need 3 open flowers. On day 2 only indices 0 and 4 are open (2 flowers). On day 3 index 2 also opens, giving 3 open flowers — enough for 3 bouquets.
Example 2
InputbloomDay = [1,10,3,10,2], m = 3, k = 2
Output-1
WhyWe need 3 bouquets × 2 flowers = 6 flowers, but the garden has only 5. No amount of waiting can create more flowers, so it is impossible.
Example 3
InputbloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output12
WhyBy day 7 the open flowers are at indices 0,1,2,3,5,6. The run 0-1-2 gives one bouquet, but index 4 is still closed, so the flowers at 5 and 6 are only a run of 2 — not enough for a second bouquet. On day 12 index 4 opens and all 7 flowers form one long run, which easily gives 2 bouquets of 3.

Asked At

1 companies
CompanyDifficulty
FlipkartMediumView all Flipkart questions →

How to Think About It

1

If you need m * k flowers and there are fewer, the answer is -1 immediately.

2

Key insight: feasibility is monotone in the day. If you can make m bouquets by day d, you can also do it by any later day. So binary search on d.

3

Check a day d greedily: scan left to right, counting consecutive bloomed flowers (bloomDay[i] <= d). Every time the run reaches k, form a bouquet and reset the run. A flower that has not bloomed also resets the run.

4

Search range: from the smallest bloom day to the largest.

5

Walkthrough: bloomDay = [1,10,3,10,2], m = 3, k = 1. Day 3: bloomed at indices 0, 2, 4 -> 3 bouquets. Day 2 gives only 2. Answer 3.

Optimal Approach

Step 1: If m * k > n, return -1.
Step 2: lo = min(bloomDay), hi = max(bloomDay).
Step 3: While lo < hi:
mid = (lo + hi) // 2
If canMake(mid): hi = mid, else lo = mid + 1.
Step 4: Return lo.

canMake(d): greedy scan counting runs of bloomed flowers.

Time: O(n log D) where D is the day range. Space: O(1).

Binary search on the day

lo = 1, hi = 10mid = 5: canMake? yeshi = 5mid = 3: canMake? yeshi = 3, answer 3Each check either keeps the current day or moves loup, shrinking the range until lo == hi.day 3 checkopenclosedopenclosedopen01234canMake(3) counts runs of open flowers; with k = 1each open flower is its own bouquet, giving 3 >= m.if m * k > n: return -1canMake(d): count runs of bloomDay[i] <= devery run of k gives one bouquet
Feasibility only improves as days pass, so we binary search for the smallest day where the greedy scan finds m bouquets.
ComplexityTime O(n log D)Space O(1)The greedy feasibility check scans the array once, and binary search over the day range [min, max] takes log D steps, where D is at most 10^9.

What Trips People Up in Real Interviews

1

Checking every day from 1 to max(bloomDay) — up to 10^9 checks.

2

Overflow computing m * k in C++/Java (10^6 * 10^5). Use a 64-bit type.

3

Forgetting to reset the run on an unbloomed flower; bouquets need adjacent flowers.

4

Using lo <= hi with hi = mid, which can loop forever. Match the loop condition to the update rule.

Solution Code

O(n log D) time
def minDays(bloomDay, m, k):
    if m * k > len(bloomDay):
        return -1

    def can_make(day):
        bouquets = run = 0
        for b in bloomDay:
            if b <= day:
                run += 1
                if run == k:
                    bouquets += 1
                    run = 0
            else:
                run = 0
        return bouquets >= m

    lo, hi = min(bloomDay), max(bloomDay)
    while lo < hi:
        mid = (lo + hi) // 2
        if can_make(mid):
            hi = mid
        else:
            lo = mid + 1
    return lo

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Minimum Number of Days to Make m Bouquets problem?

Minimum Number of Days to Make m Bouquets gives you the day each flower blooms and asks for the earliest day on which you can make `m` bouquets of `k` adjacent bloomed flowers each (or `-1`). It is a textbook "binary search on the answer" problem.

How do you solve Minimum Number of Days to Make m Bouquets?

The optimal approach runs in O(n log D) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Minimum Number of Days to Make m Bouquets?

Minimum Number of Days to Make m Bouquets is asked at Flipkart. It is a medium difficulty problem.

What are common mistakes on Minimum Number of Days to Make m Bouquets?
  • Checking every day from 1 to `max(bloomDay)` — up to `10^9` checks.
  • Overflow computing `m * k` in C++/Java (`10^6 * 10^5`). Use a 64-bit type.
  • Forgetting to reset the run on an unbloomed flower; bouquets need adjacent flowers.
  • Using `lo <= hi` with `hi = mid`, which can loop forever. Match the loop condition to the update rule.