Place k elements such that minimum distance is maximized in C++

In this problem, we are given an array of n points that lie on the same line. Our task is to place k elements of the array in such a way that the minimum distance between them is maximized.

Let’s take an example to understand the problem,

Input − array = {}

Output

To solve this problem, we will find have to find the maximum possible minimum distance. For such a problem first, we need to sort the given array and then do a binary search until we get the solution at mid.

Example

Program to show the implementation of our solution,

Live Demo

#include <bits/stdc++.h>
using namespace std;
bool canGenerateResult(int mid, int arr[], int n, int k) {
int pos = arr[0];
int elements = 1;
for (int i=1; i<n; i++){
if (arr[i] - pos >= mid){
pos = arr[i];
elements++;
if (elements == k)
return true;
}
}
return 0;
}
int maxMinDist(int arr[], int n, int k) {
sort(arr,arr+n);
int res = -1;
int left = arr[0], right = arr[n-1];
while (left < right){
int mid = (left + right)/2;
if (canGenerateResult(mid, arr, n, k)){
res = max(res, mid);
left = mid + 1;
}
else
right = mid;
}
return res;
}
int main() {
int arr[] = {3, 5, 6, 9, 1, 8};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 3;
cout<<"The maximized minimum distance is : "<<maxMinDist(arr, n, k);
return 0;
}

Output

The maximized minimum distance is : 4