![]() |
VOOZH | about |
Given an array stations[] of integers representing the position of N points along a straight line and an integer k, the task is to find the minimum value of the maximum distanced between adjacent gas stations after adding K more gas stations anywhere in between, not necessarily on an integer position.
Note: Stations are in strictly increasing order
Examples:
Input: stations[] = {1, 2, 3, 4, 5}, K = 2
Output: 1.00
Explanation: Since all gaps are already equal (1 unit each), adding extra stations in between does not reduce the maximum distance.Input: stations[] = {
3, 6, 12, 19, 33}, K = 3
Output: 6.00
Explanation: The largest gap is 14 (between 19 and 33). Adding 2 stations there splits it into ≈4.67. The next largest gap is 7 (between 12 and 19). Adding 1 station splits it into 3.5. Now the maximum gap left is 6.
The given problem can be solved by using Binary Search. The idea is to perform a binary search on the value D in the range [0, 108] where D represents the value of the maximum distance between the adjacent points after adding K points.
Step By Step Implementation:
Below is the implementation of the above approach:
0.5
Time Complexity: O(N*log M), where the value of M is 1014.
Auxiliary Space: O(1)