{"id":590,"date":"2025-07-09T18:47:30","date_gmt":"2025-07-09T13:17:30","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=590"},"modified":"2025-07-09T18:47:32","modified_gmt":"2025-07-09T13:17:32","slug":"minimum-number-of-days-to-make-m-bouquets","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/minimum-number-of-days-to-make-m-bouquets\/","title":{"rendered":"Minimum Number of Days to Make m Bouquets | Leetcode 1482 | Binary Search on Answers"},"content":{"rendered":"\n<p>The &#8220;Minimum Number\u00a0of Days to Make\u00a0m Bouquets&#8221; problem\u00a0is a popular\u00a0binary search\u00a0interview question\u00a0that tests your\u00a0ability to optimize\u00a0under constraints. <\/p>\n\n\n\n<p>In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through both\u00a0brute force and\u00a0optimal solutions, and make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/minimum-number-of-days-to-make-m-bouquets\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"0-what-does-the-problem-say\">What Does the Problem Say?<\/h3>\n\n\n\n<p>You are given an integer array&nbsp;<code>bloomDay<\/code>, an integer&nbsp;<code>m<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>You want to make&nbsp;<code>m<\/code>&nbsp;bouquets. To make a bouquet, you need to use&nbsp;<code>k<\/code>&nbsp;<strong>adjacent flowers<\/strong>&nbsp;from the garden.<\/p>\n\n\n\n<p>The garden consists of&nbsp;<code>n<\/code>&nbsp;flowers, the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;flower will bloom in the&nbsp;<code>bloomDay[i]<\/code>&nbsp;and then can be used in&nbsp;<strong>exactly one<\/strong>&nbsp;bouquet.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of days you need to wait to be able to make&nbsp;<\/em><code>m<\/code><em>&nbsp;bouquets from the garden<\/em>. If it is impossible to make m bouquets return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> bloomDay = [1,10,3,10,2], m = 3, k = 1<br><strong>Output:<\/strong> 3<br><strong>Explanation:<\/strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.<br>We need 3 bouquets each should contain 1 flower.<br>After day 1: [x, _, _, _, _]   \/\/ we can only make one bouquet.<br>After day 2: [x, _, _, _, x]   \/\/ we can only make two bouquets.<br>After day 3: [x, _, x, _, x]   \/\/ we can make 3 bouquets. The answer is 3.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> bloomDay = [1,10,3,10,2], m = 3, k = 2<br><strong>Output:<\/strong> -1<br><strong>Explanation:<\/strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3<br><strong>Output:<\/strong> 12<br><strong>Explanation:<\/strong> We need 2 bouquets each should have 3 flowers.<br>Here is the garden after the 7 and 12 days:<br>After day 7: [x, x, x, x, _, x, x]<br>We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.<br>After day 12: [x, x, x, x, x, x, x]<br>It is obvious that we can make two bouquets in different ways.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>bloomDay.length == n<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9<\/sup><\/code><\/li>\n\n\n\n<li><code>1 &lt;= m &lt;= 10<sup>6<\/sup><\/code><\/li>\n\n\n\n<li><code>1 &lt;= k &lt;= n<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-brute-force-solution-try-every-possible-day\">Brute Force Solution (Try Every Possible Day)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Day:<\/strong><br>Try every possible day from the earliest blooming flower to the latest.<\/li>\n\n\n\n<li><strong>Check If Bouquets Can Be Made:<\/strong><br>For each day, check if it is possible to make\u00a0<code>m<\/code>\u00a0bouquets, each with\u00a0<code>k<\/code>\u00a0adjacent flowers that have bloomed by that day.<\/li>\n\n\n\n<li><strong>Return the First Valid Day:<\/strong><br>The first day for which it\u2019s possible is our answer.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible day, so we\u2019re sure to find the minimum day that works.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\nclass Solution:\n    def canMakeBouquet(self, bloomDay: List[int], m: int, k: int, day: int) -&gt; bool:\n        bouquets = 0\n        flowers = 0\n        for bloom in bloomDay:\n            if bloom &lt;= day:\n                flowers += 1\n                if flowers == k:\n                    bouquets += 1\n                    flowers = 0\n            else:\n                flowers = 0\n        return bouquets &gt;= m\n\n    def minDays(self, bloomDay: List[int], m: int, k: int) -&gt; int:\n        n = len(bloomDay)\n        if m * k &gt; n:\n            return -1  # Not enough flowers\n\n        min_day = min(bloomDay)\n        max_day = max(bloomDay)\n\n        for day in range(min_day, max_day + 1):\n            if self.canMakeBouquet(bloomDay, m, k, day):\n                return day\n\n        return -1\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">from<\/span><span style=\"color: #E1E4E8\"> typing <\/span><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">canMakeBouquet<\/span><span style=\"color: #E1E4E8\">(self, bloomDay: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], m: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, k: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, day: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">bool<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        bouquets <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        flowers <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> bloom <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> bloomDay:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> bloom <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> day:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                flowers <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> flowers <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> k:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    bouquets <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    flowers <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                flowers <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> bouquets <\/span><span style=\"color: #F97583\">&gt;=<\/span><span style=\"color: #E1E4E8\"> m<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">minDays<\/span><span style=\"color: #E1E4E8\">(self, bloomDay: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], m: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, k: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(bloomDay)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> m <\/span><span style=\"color: #F97583\">*<\/span><span style=\"color: #E1E4E8\"> k <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">  <\/span><span style=\"color: #6A737D\"># Not enough flowers<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        min_day <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">min<\/span><span style=\"color: #E1E4E8\">(bloomDay)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        max_day <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(bloomDay)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> day <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(min_day, max_day <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.canMakeBouquet(bloomDay, m, k, day):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> day<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each possible day from the earliest to the latest bloom, check if you can make the required bouquets.<\/li>\n\n\n\n<li>The helper function\u00a0<code>canMakeBouquet<\/code>\u00a0counts how many bouquets can be made by that day.<\/li>\n\n\n\n<li>If you find a day where it\u2019s possible, return it.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>bloomDay = [1], m = 3, k = 1<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try day = 1: Only 1 flower has bloomed, not enough.<\/li>\n\n\n\n<li>Try day = 2: Two flowers have bloomed, still not enough.<\/li>\n\n\n\n<li>Try day = 3: Three flowers have bloomed (positions 0, 2, 4) \u2192 can make 3 bouquets.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>3<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O((max(bloomDay) &#8211; min(bloomDay)) * n)<br>For each day, we scan the whole array.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small inputs, but it\u2019s too slow for large values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search on Days:<\/strong><br>The answer lies between the earliest and latest bloom days. Use binary search to find the minimum day.<\/li>\n\n\n\n<li><strong>Check If Bouquets Can Be Made:<\/strong><br>For each mid value (day), check if it\u2019s possible to make\u00a0<code>m<\/code>\u00a0bouquets.<\/li>\n\n\n\n<li><strong>Adjust Search Window:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If it\u2019s possible, try an earlier day.<\/li>\n\n\n\n<li>If not, try a later day.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The function for checking if bouquets can be made is monotonic: as the day increases, it becomes easier to make bouquets. Binary search is perfect for this.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\nclass Solution:\n    def canMakeBouquet(self, bloomDay: List[int], m: int, k: int, day: int) -&gt; bool:\n        total = 0\n        count = 0\n        for i in range(len(bloomDay)):\n            if bloomDay[i] &lt;= day:\n                count += 1\n                if count == k:\n                    total += 1\n                    count = 0\n            else:\n                count = 0\n        return total &gt;= m\n\n    def minDays(self, bloomDay: List[int], m: int, k: int) -&gt; int:\n        if m * k &gt; len(bloomDay):\n            return -1\n\n        low, high = min(bloomDay), max(bloomDay)\n        ans = -1\n\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            if self.canMakeBouquet(bloomDay, m, k, mid):\n                ans = mid\n                high = mid - 1\n            else:\n                low = mid + 1\n\n        return ans\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">from<\/span><span style=\"color: #E1E4E8\"> typing <\/span><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">canMakeBouquet<\/span><span style=\"color: #E1E4E8\">(self, bloomDay: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], m: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, k: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, day: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">bool<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        total <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        count <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(bloomDay)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> bloomDay[i] <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> day:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                count <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> count <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> k:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    total <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    count <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                count <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> total <\/span><span style=\"color: #F97583\">&gt;=<\/span><span style=\"color: #E1E4E8\"> m<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">minDays<\/span><span style=\"color: #E1E4E8\">(self, bloomDay: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], m: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">, k: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> m <\/span><span style=\"color: #F97583\">*<\/span><span style=\"color: #E1E4E8\"> k <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(bloomDay):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low, high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">min<\/span><span style=\"color: #E1E4E8\">(bloomDay), <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(bloomDay)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.canMakeBouquet(bloomDay, m, k, mid):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> ans<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between the earliest and latest bloom days.<\/li>\n\n\n\n<li>For each mid day, check if you can make the required bouquets.<\/li>\n\n\n\n<li>If you can, try a smaller day.<\/li>\n\n\n\n<li>If not, try a larger day.<\/li>\n\n\n\n<li>Return the minimum valid day found.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>bloomDay = [1], m = 3, k = 1<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 1, high = 10<\/li>\n\n\n\n<li>mid = 5: Can make 3 bouquets? Yes \u2192 try smaller (high = 4)<\/li>\n\n\n\n<li>mid = 2: Can make 2 bouquets? No \u2192 try larger (low = 3)<\/li>\n\n\n\n<li>mid = 3: Can make 3 bouquets? Yes \u2192 try smaller (high = 2)<\/li>\n<\/ul>\n\n\n\n<p>Loop ends, answer is 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n * log(max(bloomDay) &#8211; min(bloomDay)))<br>Binary search on days, checking all flowers for each guess.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays and high values. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"15-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Minimum Number of Days to Make m Bouquets&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Minimum Number\u00a0of Days to Make\u00a0m Bouquets&#8221; problem\u00a0is a popular\u00a0binary search\u00a0interview question\u00a0that tests your\u00a0ability to optimize\u00a0under constraints. In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through both\u00a0brute force and\u00a0optimal solutions, and make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations. Here&#8217;s the [Problem Link] to begin with. What Does the Problem Say? You are given<\/p>\n","protected":false},"author":1,"featured_media":591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[28,19],"class_list":{"0":"post-590","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-binary-search-on-answers","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/minimum-number-of-days-to-make-m-bouquets-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions\/592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/591"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}