Given an array representing n positions along a straight line. Find k (where k <= n) elements from the array such that the minimum distance between any two (consecutive points among the k points) is maximized.
Examples :
Input: arr[] = {1, 2, 8, 4, 9}, k = 3
Output: 3
Explanation: Largest minimum distance = 3
3 elements arranged at positions 1, 4 and 8,
Resulting in a minimum distance of 3
Input: arr[] = {1, 2, 7, 5, 11, 12}, k = 3
Output: 5
Explanation: Largest minimum distance = 5
3 elements arranged at positions 1, 7 and 12,
resulting in a minimum distance of 5 (between 7 and 12)
A Naive Solution is to consider all subsets of size 3 and find the minimum distance for every subset. Finally, return the largest of all minimum distances.
An Efficient Solution is based on Binary Search. We first sort the array. Now we know maximum possible value result is arr[n-1] – arr[0] (for k = 2). We do a binary search for maximum result for given k. We start with the middle of the maximum possible result. If the middle is a feasible solution, we search on the right half of mid. Else we search is left half. To check feasibility, we place k elements under given mid-distance.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isFeasible( 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 largestMinDist( int arr[], int n, int k)
{
sort(arr, arr + n);
int res = -1;
int left = 1, right = arr[n - 1];
while (left < right) {
int mid = (left + right) / 2;
if (isFeasible(mid, arr, n, k)) {
res = max(res, mid);
left = mid + 1;
}
else
right = mid;
}
return res;
}
int main()
{
int arr[] = { 1, 2, 8, 4, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 3;
cout << largestMinDist(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static boolean isFeasible( 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 false ;
}
static int largestMinDist( int arr[], int n, int k)
{
Arrays.sort(arr);
int res = - 1 ;
int left = 1 , right = arr[n - 1 ];
while (left < right) {
int mid = (left + right) / 2 ;
if (isFeasible(mid, arr, n, k)) {
res = Math.max(res, mid);
left = mid + 1 ;
}
else
right = mid;
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 8 , 4 , 9 };
int n = arr.length;
int k = 3 ;
System.out.print(largestMinDist(arr, n, k));
}
}
|
Python3
def isFeasible(mid, arr, n, k):
pos = arr[ 0 ]
elements = 1
for i in range ( 1 , n, 1 ):
if (arr[i] - pos > = mid):
pos = arr[i]
elements + = 1
if (elements = = k):
return True
return 0
def largestMinDist(arr, n, k):
arr.sort(reverse = False )
res = - 1
left = 1
right = arr[n - 1 ]
while (left < right):
mid = (left + right) / 2
if (isFeasible(mid, arr, n, k)):
res = max (res, mid)
left = mid + 1
else :
right = mid
return res
if __name__ = = '__main__' :
arr = [ 1 , 2 , 8 , 4 , 9 ]
n = len (arr)
k = 3
print (largestMinDist(arr, n, k))
|
C#
using System;
public class GFG {
static bool isFeasible( 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 false ;
}
static int largestMinDist( int [] arr, int n, int k)
{
Array.Sort(arr);
int res = -1;
int left = 1, right = arr[n - 1];
while (left < right) {
int mid = (left + right) / 2;
if (isFeasible(mid, arr, n, k)) {
res = Math.Max(res, mid);
left = mid + 1;
}
else
right = mid;
}
return res;
}
public static void Main()
{
int [] arr = { 1, 2, 8, 4, 9 };
int n = arr.Length;
int k = 3;
Console.WriteLine(largestMinDist(arr, n, k));
}
}
|
Javascript
<script>
function isFeasible(mid, arr, n, k)
{
let pos = arr[0];
let elements = 1;
for (let i = 1; i < n; i++) {
if (arr[i] - pos >= mid) {
pos = arr[i];
elements++;
if (elements == k)
return true ;
}
}
return false ;
}
function largestMinDist(arr, n, k)
{
arr.sort( function (a, b){ return a - b});
let res = -1;
let left = 1, right = arr[n - 1];
while (left < right) {
let mid = parseInt((left + right) / 2, 10);
if (isFeasible(mid, arr, n, k)) {
res = Math.max(res, mid);
left = mid + 1;
}
else
right = mid;
}
return res;
}
let arr = [ 1, 2, 8, 4, 9 ];
let n = arr.length;
let k = 3;
document.write(largestMinDist(arr, n, k));
</script>
|
PHP
<?php
function isFeasible( $mid , $arr ,
$n , $k )
{
$pos = $arr [0];
$elements = 1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i ] - $pos >= $mid )
{
$pos = $arr [ $i ];
$elements ++;
if ( $elements == $k )
return true;
}
}
return 0;
}
function largestMinDist( $arr , $n , $k )
{
sort( $arr );
$res = -1;
$left = 1;
$right = $arr [ $n - 1];
while ( $left < $right )
{
$mid = ( $left + $right ) / 2;
if (isFeasible( $mid , $arr ,
$n , $k ))
{
$res = max( $res , $mid );
$left = $mid + 1;
}
else
$right = $mid ;
}
return $res ;
}
$arr = array (1, 2, 8, 4, 9);
$n = sizeof( $arr );
$k = 3;
echo largestMinDist( $arr , $n , $k );
?>
|
Time Complexity: O(n log m) , where n is length of array and m is the maximum element of the array.
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Sep, 2023
Like Article
Save Article