{"id":613,"date":"2025-07-10T13:20:43","date_gmt":"2025-07-10T07:50:43","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=613"},"modified":"2025-07-10T13:20:45","modified_gmt":"2025-07-10T07:50:45","slug":"minimize-max-distance-to-gas-station","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/","title":{"rendered":"Minimize Max Distance to Gas Station | GFG Practice | Optimal Binary Search"},"content":{"rendered":"\n<p>The &#8220;Minimize Max Distance to Gas Station&#8221; problem is a classic example of using binary search on real numbers (floating point search) to optimize distances. <\/p>\n\n\n\n<p>In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/minimize-max-distance-to-gas-station\/1\" 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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement-\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>We have a horizontal number line. On that number line, we have gas&nbsp;<strong>stations&nbsp;<\/strong>at positions stations[0], stations[1], &#8230;, stations[n-1], where&nbsp;<strong>n&nbsp;<\/strong>is the&nbsp;size of the stations array. Now, we add&nbsp;<strong>k<\/strong>&nbsp;more gas stations so that&nbsp;<strong>d<\/strong>, the maximum distance between adjacent gas stations, is minimized. We have to find the smallest possible value of d. Find the answer&nbsp;<strong>exactly<\/strong>&nbsp;to 2 decimal places.<br><strong>Note<\/strong>:&nbsp;<strong><code>stations<\/code><\/strong>&nbsp;is in a&nbsp;<strong>strictly increasing<\/strong>&nbsp;order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1-\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:\n<\/strong>n = 10\nstations[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nk = 9\n<strong>Output:<\/strong> 0.50\n<strong>Explanation: <\/strong>Each of the 9 stations can be added mid way between all the existing adjacent stations.<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2-\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:\n<\/strong>n = 10\nstations[] = <code>[3, 6, 12, 19, 33, 44, 67, 72, 89, 95]<\/code> <br>k = 2 <br><strong>Output:<\/strong> 14.00 <br><strong>Explanation: <\/strong>Construction of gas stations at 8th(between 72 and 89) and 6th(between 44 and 67) locations.<\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><br>You don&#8217;t need to read input or print anything. Your task is to complete the function&nbsp;<strong>findSmallestMaxDist()&nbsp;<\/strong>which takes a list of stations and integer k as inputs and returns the smallest possible value of d. Find the answer&nbsp;<strong>exactly<\/strong>&nbsp;to 2 decimal places.<\/p>\n\n\n\n<p><strong>Constraint:<\/strong><br>10 &lt;= n &lt;= 10000<sup>&nbsp;<\/sup><br>0 &lt;= stations[i] &lt;= 10<sup>9&nbsp;<\/sup><br>0 &lt;= k &lt;= 10<sup>5<\/sup><\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-1f94ee23-8806-4628-8c1f-79495bb9f8c9\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#0-problem-statement-\" style=\"\">Problem Statement<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#1-example-1-\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#2-example-2-\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#3-brute-force-solution-try-every-possible-distance\" style=\"\">Brute Force Solution (Try Every Possible Distance)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#4-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#5-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#6-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#7-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#8-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#9-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#10-optimal-solution-binary-search-on-answer\" style=\"\">Optimal Solution (Binary Search on Answer)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#11-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#12-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#16-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimize-max-distance-to-gas-station\/#17-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-brute-force-solution-try-every-possible-distance\">Brute Force Solution (Try Every Possible Distance)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-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 Possible Distance:<\/strong><br>Start from a small distance (like 0.01) and increase by small steps up to the largest gap between stations.<\/li>\n\n\n\n<li><strong>Check If Placement Is Possible:<\/strong><br>For each distance, check if you can add at most&nbsp;<code>k<\/code>&nbsp;stations so that no gap is larger than this distance.<\/li>\n\n\n\n<li><strong>Return the First Valid Distance:<\/strong><br>The first distance for which placement is possible is our answer.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible distance, so we\u2019re sure to find the smallest one that works.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large inputs or high precision.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-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=\"class Solution:\n    def canPlace(self, stations, k, d):\n        needed = 0\n        for i in range(1, len(stations)):\n            gap = stations[i] - stations[i - 1]\n            needed += int(gap \/\/ d)  # How many extra stations needed in this gap\n        return needed &lt;= k\n\n    def findSmallestMaxDist(self, stations, k):\n        max_gap = max(stations[i + 1] - stations[i] for i in range(len(stations) - 1))\n        d = 0.01\n        while d &lt;= max_gap:\n            if self.canPlace(stations, k, d):\n                return round(d, 2)  # Return as float, rounded to 2 decimals\n            d += 0.01\n        return round(max_gap, 2)\" 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\">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\">canPlace<\/span><span style=\"color: #E1E4E8\">(self, stations, k, d):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        needed <\/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\">1<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(stations)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            gap <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> stations[i] <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> stations[i <\/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\">            needed <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">(gap <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> d)  <\/span><span style=\"color: #6A737D\"># How many extra stations needed in this gap<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> needed <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> k<\/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\">findSmallestMaxDist<\/span><span style=\"color: #E1E4E8\">(self, stations, k):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        max_gap <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(stations[i <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> stations[i] <\/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\">(stations) <\/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\">        d <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0.01<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> d <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> max_gap:<\/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\">.canPlace(stations, k, d):<\/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: #79B8FF\">round<\/span><span style=\"color: #E1E4E8\">(d, <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">)  <\/span><span style=\"color: #6A737D\"># Return as float, rounded to 2 decimals<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            d <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0.01<\/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: #79B8FF\">round<\/span><span style=\"color: #E1E4E8\">(max_gap, <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">)<\/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=\"6-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each possible distance&nbsp;<code>d<\/code>, check if you can add at most&nbsp;<code>k<\/code>&nbsp;stations so that no gap is bigger than&nbsp;<code>d<\/code>.<\/li>\n\n\n\n<li>The helper function&nbsp;<code>canPlace<\/code>&nbsp;counts how many new stations are needed for a given&nbsp;<code>d<\/code>.<\/li>\n\n\n\n<li>Return the first&nbsp;<code>d<\/code>&nbsp;that works.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>stations = [1], k = 9<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try d = 0.01, 0.02, &#8230;, 1.00<\/li>\n\n\n\n<li>At d = 1.00, you need 9 new stations (at positions 2, 3, &#8230;, 9), which is allowed.<\/li>\n\n\n\n<li>Return 1.00<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>1.00<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O((max_gap \/ step) * n)<br>For each possible distance, we check all gaps.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small inputs or low precision, but it\u2019s too slow for large values or high precision.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-optimal-solution-binary-search-on-answer\">Optimal Solution (Binary Search on Answer)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search on the answer (floating point search):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search on Distance:<\/strong><br>The answer lies between 0 and the largest gap between stations. Use binary search to find the smallest maximum distance.<\/li>\n\n\n\n<li><strong>Check If Placement Is Possible:<\/strong><br>For each mid value (distance), check if you can add at most&nbsp;<code>k<\/code>&nbsp;stations so that no gap is bigger than this distance.<\/li>\n\n\n\n<li><strong>Adjust Search Window:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If placement is possible, try a smaller distance.<\/li>\n\n\n\n<li>If not, try a larger distance.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The function is monotonic: as the distance increases, it becomes easier to place all stations. Binary search is perfect for this.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-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=\"class Solution:\n    def canPlace(self, stations, k, dist):\n        needed = 0\n        for i in range(1, len(stations)):\n            gap = stations[i] - stations[i-1]\n            # Number of additional stations needed in this gap\n            needed += int(gap \/ dist)\n        return needed &lt;= k\n\n    def findSmallestMaxDist(self, stations, k):\n        left, right = 0.0, max(stations[i+1] - stations[i] for i in range(len(stations)-1))\n        # Binary search with precision up to 1e-6\n        while right - left &gt; 1e-6:\n            mid = (left + right) \/ 2\n            if self.canPlace(stations, k, mid):\n                right = mid\n            else:\n                left = mid\n        # Round to 2 decimal places as required\n        return round(right, 2)\" 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\">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\">canPlace<\/span><span style=\"color: #E1E4E8\">(self, stations, k, dist):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        needed <\/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\">1<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(stations)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            gap <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> stations[i] <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> stations[i<\/span><span style=\"color: #F97583\">-<\/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: #6A737D\"># Number of additional stations needed in this gap<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            needed <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">(gap <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> dist)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> needed <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> k<\/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\">findSmallestMaxDist<\/span><span style=\"color: #E1E4E8\">(self, stations, k):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        left, right <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0.0<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(stations[i<\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> stations[i] <\/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\">(stations)<\/span><span style=\"color: #F97583\">-<\/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: #6A737D\"># Binary search with precision up to 1e-6<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> right <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> left <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1e-6<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (left <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> right) <\/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\">.canPlace(stations, k, mid):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                right <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid<\/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\">                left <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Round to 2 decimal places as required<\/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: #79B8FF\">round<\/span><span style=\"color: #E1E4E8\">(right, <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">)<\/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=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between 0 and the maximum gap.<\/li>\n\n\n\n<li>For each mid distance, check if you can add at most&nbsp;<code>k<\/code>&nbsp;stations.<\/li>\n\n\n\n<li>If you can, try a smaller distance.<\/li>\n\n\n\n<li>If not, try a larger distance.<\/li>\n\n\n\n<li>Keep searching until the difference is very small (high precision).<\/li>\n\n\n\n<li>Return the smallest valid distance, rounded to 2 decimals.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>stations = [1], k = 9<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>left = 0.0, right = 9.0<\/li>\n\n\n\n<li>mid = 4.5, need 1 station (not enough, try smaller)<\/li>\n\n\n\n<li>mid = 2.25, need 3 stations (not enough, try smaller)<\/li>\n\n\n\n<li>&#8230;<\/li>\n\n\n\n<li>mid = 1.00, need 9 stations (just right)<\/li>\n\n\n\n<li>Loop ends, answer is 1.00<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>1.00<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(n * log(max_gap \/ precision))<br>Binary search on distance, checking all gaps for each guess.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays and high precision. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Minimize Max Distance to Gas Station&#8221; problem is a great example of using binary search on real numbers to optimize placement. Start with brute force to understand the basics, then use binary search for best performance and precision.<\/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;Minimize Max Distance to Gas Station&#8221; problem is a classic example of using binary search on real numbers (floating point search) to optimize distances. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n","protected":false},"author":1,"featured_media":614,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[28,18],"class_list":{"0":"post-613","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-binary-search-on-answers","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/minimize-max-distance-to-gas-station-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\/613","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=613"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"predecessor-version":[{"id":616,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/613\/revisions\/616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/614"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}