Minimize the maximum distance between adjacent points after adding K points anywhere in between | Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code
function isPossible ( D , arr , N , K ) { let used = 0 ; for ( let i = 0 ; i < N - 1 ; ++ i ) { used += Math . floor ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } function minMaxDist ( stations , N , K ) { let low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { let mid = ( low + high ) / 2 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low . toFixed ( 1 ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let K = 9 ; let N = arr . length ; document . write ( minMaxDist ( arr , N , K ) ) ;