Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Minimise Maximum Distance To Go To Gas Station

Medium Acceptance 64.00% Points 30.00

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.

Hints
Hint 1
N/A
Constraints
  • 2 = stations.length = 105
  • 0 = stations[i] = 10?
  • 0 = k = 105
  • stations is sorted in ascending order
Companies
Apple
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions