Find the earliest day you can make m bouquets of k adjacent flowers

Quick Overview

Given the day each flower in a row blooms, find the earliest day on which m bouquets of k adjacent bloomed flowers can be made, or report that it is impossible. The problem tests binary search on the answer with a monotone greedy feasibility check.

Find the earliest day you can make m bouquets of k adjacent flowers

Company: Yext

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `bloom_day`, an integer `m` and an integer `k`. You want to make `m` bouquets. To make a bouquet, you need to use `k` adjacent flowers from the garden. The garden has `n` flowers in a row. Flower `i` blooms on day `bloom_day[i]`, and after that it can be used in at most one bouquet. Return the minimum number of days you need to wait to be able to make `m` bouquets from the garden. If it is impossible to make `m` bouquets, return `-1`. ### Function Signature ```python def min_days(bloom_day: list[int], m: int, k: int) -> int: ``` ### Rules - Flower `i` is available on day `d` when `bloom_day[i] <= d`, and it stays available after that. - A bouquet is a block of `k` consecutive positions in `bloom_day`, all available on the chosen day. Two bouquets cannot share a flower. - Return the smallest day on which `m` bouquets can be made, or `-1` if no day works. ### Constraints - `len(bloom_day) == n` - `1 <= n <= 10^5` - `1 <= bloom_day[i] <= 10^9` - `1 <= m <= 10^6` - `1 <= k <= n` ### Examples ```text Input: bloom_day = [1,10,3,10,2], m = 3, k = 1 Output: 3 ``` Each bouquet needs one flower. By day 3 the flowers that bloom on days 1, 3 and 2 are open, which gives 3 bouquets. On day 2 only two flowers are open. ```text Input: bloom_day = [1,10,3,10,2], m = 3, k = 2 Output: -1 ``` Three bouquets of 2 adjacent flowers need 6 flowers, and the garden has only 5. ```text Input: bloom_day = [7,7,7,7,12,7,7], m = 2, k = 3 Output: 12 ``` On day 7 every flower except the one at index 4 is open, so the open flowers form runs of length 4 and 2. That allows only one bouquet of 3. On day 12 all 7 flowers form one run, which gives 2 bouquets.

Overview: Given the day each flower in a row blooms, find the earliest day on which m bouquets of k adjacent bloomed flowers can be made, or report that it is impossible. The problem tests binary search on the answer with a monotone greedy feasibility check.

Yext logo
Yext
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array bloom_day, an integer m and an integer k.

You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden has n flowers in a row. Flower i blooms on day bloom_day[i], and after that it can be used in at most one bouquet.

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets, return -1.

Function Signature

def min_days(bloom_day: list[int], m: int, k: int) -> int:

Rules

  • Flower i is available on day d when bloom_day[i] <= d , and it stays available after that.
  • A bouquet is a block of k consecutive positions in bloom_day , all available on the chosen day. Two bouquets cannot share a flower.
  • Return the smallest day on which m bouquets can be made, or -1 if no day works.

Constraints

  • len(bloom_day) == n
  • 1 <= n <= 10^5
  • 1 <= bloom_day[i] <= 10^9
  • 1 <= m <= 10^6
  • 1 <= k <= n

Examples

Input: bloom_day = [1,10,3,10,2], m = 3, k = 1
Output: 3

Each bouquet needs one flower. By day 3 the flowers that bloom on days 1, 3 and 2 are open, which gives 3 bouquets. On day 2 only two flowers are open.

Input: bloom_day = [1,10,3,10,2], m = 3, k = 2
Output: -1

Three bouquets of 2 adjacent flowers need 6 flowers, and the garden has only 5.

Input: bloom_day = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12

On day 7 every flower except the one at index 4 is open, so the open flowers form runs of length 4 and 2. That allows only one bouquet of 3. On day 12 all 7 flowers form one run, which gives 2 bouquets.