Minimize the maximum distance between adjacent points after adding K points anywhere in between
Last Updated :
20 Sep, 2023
Given an array arr[] of N 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 distance between adjacent points after adding K points anywhere in between, not necessarily on an integer position.
Examples:
Input: arr[] = {2, 4, 8, 10}, K = 1
Output: 2
Explanation: A point at position 6 can be added. So the new array of points become {2, 4, 6, 8, 10} and the maximum distance between two adjacent points is 2 which is minimum possible.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, K = 9
Output: 0.5
Approach: 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. Follow the steps below to solve the given problem:
- Initialize variables, low = 1 and high = 108, where low represents the lower bound and high represents the upper bound of the binary search.
- Create a function isPossible(), which returns the boolean value of whether it is possible to add K points in the array such that the maximum distance between adjacent points is D. It is based on the observation that for two adjacent points (i, j), the number of points required to be placed in their middle such that the maximum distance between them is D = (j -i)/D.
- Therefore, traverse the range using the binary search algorithm discussed here, and if for the mid-value D in the range [X, Y], if isPossible(D) is false, then iterate in the upper half of the range i.e, [D, Y]. Otherwise, iterate on the lower half i.e, [X, D].
- Iterate a loop until (high – low) > 10-6.
- The value stored in low is the required answer.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool isPossible( double D, int arr[],
int N, int K)
{
int used = 0;
for ( int i = 0; i < N - 1; ++i) {
used += ( int )((arr[i + 1]
- arr[i])
/ D);
}
return used <= K;
}
double minMaxDist( int stations[], int N, int K)
{
double low = 0, high = 1e8;
while (high - low > 1e-6) {
double mid = (low + high) / 2.0;
if (isPossible(mid, stations, N, K)) {
high = mid;
}
else {
low = mid;
}
}
return low;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int K = 9;
int N = sizeof (arr) / sizeof (arr[0]);
cout << minMaxDist(arr, N, K);
return 0;
}
|
Java
import java.math.BigDecimal;
class GFG {
public static boolean isPossible( double D, int arr[],
int N, int K)
{
int used = 0 ;
for ( int i = 0 ; i < N - 1 ; ++i) {
used += ( int ) ((arr[i + 1 ] - arr[i]) / D);
}
return used <= K;
}
public static double minMaxDist( int stations[], int N, int K)
{
double low = 0 , high = 1e8;
while (high - low > 1e- 6 ) {
double mid = (low + high) / 2.0 ;
if (isPossible(mid, stations, N, K)) {
high = mid;
}
else {
low = mid;
}
}
return low;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int K = 9 ;
int N = arr.length;
System.out.printf( "%.1f" , minMaxDist(arr, N, K));
}
}
|
Python3
def isPossible(D, arr, N, K) :
used = 0 ;
for i in range (N - 1 ) :
used + = int ((arr[i + 1 ] - arr[i]) / D);
return used < = K;
def minMaxDist(stations, N, K) :
low = 0 ; high = 1e8 ;
while (high - low > 1e - 6 ) :
mid = (low + high) / 2.0 ;
if (isPossible(mid, stations, N, K)) :
high = mid;
else :
low = mid;
return round (low, 2 );
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
K = 9 ;
N = len (arr);
print (minMaxDist(arr, N, K));
|
C#
using System;
public class GFG {
public static bool isPossible( double D, int []arr,
int N, int K)
{
int used = 0;
for ( int i = 0; i < N - 1; ++i) {
used += ( int ) ((arr[i + 1] - arr[i]) / D);
}
return used <= K;
}
public static double minMaxDist( int []stations, int N, int K)
{
double low = 0, high = 1e8;
while (high - low > 1e-6) {
double mid = (low + high) / 2.0;
if (isPossible(mid, stations, N, K)) {
high = mid;
}
else {
low = mid;
}
}
return low;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int K = 9;
int N = arr.Length;
Console.Write( "{0:F1}" , minMaxDist(arr, N, K));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N*log M), where the value of M is 1014.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...