Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Minimise Maximum Distance To Go To Gas Station

Medium Acceptance 64% Points 30

You are given a sorted array stations[] representing the positions of existing gas stations on a number line, and an integer k representing the number of additional gas stations you can add. Your task is to add exactly k new gas stations anywhere on the number line such that the maximum distance between any two adjacent gas stations is minimized. Return the minimized maximum distance.

Examples
Example 1

Example 1

Input: stations = [1,2,3,4,5], k = 4

Output: 0.5

Explanation: By adding gas stations between each pair, the distances can be reduced. The maximum distance between adjacent stations becomes 0.5, which is the minimum possible.

Example 2

Example 2

Input: stations = [1,10], k = 1

Output: 4.5

Explanation: Add one gas station at position 5.5. The distances become 4.5 and 4.5. The maximum distance is minimized to 4.5.

Example 3

Example 3

Input: stations = [1,5,10], k = 2

Output: 2.5

Explanation: Add gas stations at positions 3 and 7.5. The distances become 2, 2.5, and 2.5. The maximum distance is 2.5, which is minimized.

Constraints
  • 2 = stations.length = 105
  • 0 = stations[i] = 10?
  • 0 = k = 105
  • stations is sorted in ascending order
Topics
Binary Search
Companies
Apple
Added 19 Jul 2026 64% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3 4 5
1
1.00