Minimize Max Distance to Gas Station

hard

Gas stations already stand on a horizontal number line at positions arr[0] < arr[1] < ... < arr[n-1]. You are given k more stations to build, and you may place them anywhere on the line — fractional positions are allowed.

After you build them, let d be the maximum distance between two adjacent stations. Your job is to place the k new stations so that d is as small as possible.

Return the smallest possible value of d. Answers are checked to 6 decimal places.

Note: arr is strictly increasing — every adjacent pair already defines a positive gap.

Hints

Adding a station only helps if it splits the current longest gap — and inside any single gap, evenly spaced stations are always best.
Do not decide where the stations go. Guess a distance d and ask instead: how many stations would I need so that no gap exceeds d? A gap of length g needs ceil(g / d) - 1 of them.
That count only grows as d shrinks — feasibility is monotonic. Binary search on d itself over the real number line, stopping once the search window drops below 1e-6 (about 100 halvings).

Common doubts

Yes. Positions are real numbers, which is why the answer is a decimal like 4.666667 rather than an integer.
All four gaps are already length 1. Two new stations can shrink two of the gaps, but the other two remain at length 1 — the maximum does not move.
Either loop until hi - lo drops below the required precision (use 1e-7 here, tighter than the 1e-6 that is checked), or simply run a fixed 100 iterations — each halving doubles the precision, so 100 is far more than enough.
No stations get added, so the answer is the largest existing gap — a good sanity check for your feasibility function.

Interview follow-ups

Yes — after the binary search finds d, walk each gap g and drop ceil(g / d) - 1 stations evenly spaced inside it.
The same feasibility check works — replace the station count per gap with its cost and binary search on d unchanged. Monotonicity is all the search needs.
Aggressive Cows (maximize the minimum spacing), Split Array Largest Sum, and Koko Eating Bananas — the whole minimize-the-maximum / maximize-the-minimum family shares this exact skeleton.

Fun facts

  • The technique here is sometimes called parametric search: turn an optimization problem into a family of yes/no questions, then binary search the parameter.
  • Because the answer is a real number, the binary search never lands on it exactly — it corners the answer inside an interval smaller than the precision anyone cares about. Seasoned contestants just run a fixed 100 halvings and stop worrying.
  • The same math places water tables along a marathon route and cell towers along a highway: minimize the worst dry stretch, not the average one.

Asked at

GoogleAmazonUberMicrosoftFlipkart
Frequently Sometimes Occasionally
Example 1
Input: arr = [1, 2, 3, 4, 5], k = 2
Output: 1.000000
Every gap is already exactly 1. Two new stations can split two of the four gaps, but the other two stay at length 1 — the maximum cannot drop below 1.
Example 2
Input: arr = [3, 6, 12, 19, 33], k = 3
Output: 6.000000
Put 2 stations in the biggest gap (14, between 19 and 33) to cut it into pieces of about 4.67, and 1 station in the gap of 7 (between 12 and 19) to make pieces of 3.5. The largest remaining gap is 6 (between 6 and 12).
Constraints

- 1 <= arr.size() <= 10^5 - 0 <= arr[i] <= 10^6 - 0 <= k <= 10^5 - arr is strictly increasing

Solve this problem →