Place the prisoners into cells to maximize the minimum difference between any two
Given an array cell[] of N elements, which represent the positions of the cells in a prison. Also, given an integer P which is the number of prisoners, the task is to place all the prisoners in the cells in an ordered manner such that the minimum distance between any two prisoners is as large as possible. Finally, print the maximized distance.
Examples:
Input: cell[] = {1, 2, 8, 4, 9}, P = 3
Output: 3
The three prisoners will be placed at the cells
numbered 1, 4 and 8 with the minimum distance 3
which is the maximum possible.
Input: cell[] = {10, 12, 18}, P = 2
Output: 8
The three possible placements are {10, 12}, {10, 18} and {12, 18}.
Approach: This problem can be solved using binary search. As the minimum distance between two cells in which prisoners will be kept has to be maximized, the search space will be of distance, starting from 0 (if two prisoners are kept in the same cell) and ending at cell[N – 1] – cell[0] (if one prisoner is kept in the first cell, and the other one is kept in the last cell).
Initialize L = 0 and R = cell[N – 1] – cell[0] then apply the binary search. For every mid, check whether the prisoners can be placed such that the minimum distance between any two prisoners is at least mid
- If yes then try to increase this distance in order to maximize the answer and check again.
- If not then try to decrease the distance.
- Finally, print the maximized distance.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canPlace( int a[], int n, int p, int sep)
{
int prisoners_placed = 1;
int last_prisoner_placed = a[0];
for ( int i = 1; i < n; i++) {
int current_cell = a[i];
if (current_cell - last_prisoner_placed >= sep) {
prisoners_placed++;
last_prisoner_placed = current_cell;
if (prisoners_placed == p) {
return true ;
}
}
}
return false ;
}
int maxDistance( int cell[], int n, int p)
{
sort(cell, cell + n);
int start = 0;
int end = cell[n - 1] - cell[0];
int ans = 0;
while (start <= end) {
int mid = start + ((end - start) / 2);
if (canPlace(cell, n, p, mid)) {
ans = mid;
start = mid + 1;
}
else {
end = mid - 1;
}
}
return ans;
}
int main()
{
int cell[] = { 1, 2, 8, 4, 9 };
int n = sizeof (cell) / sizeof ( int );
int p = 3;
cout << maxDistance(cell, n, p);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean canPlace( int a[], int n, int p, int sep)
{
int prisoners_placed = 1 ;
int last_prisoner_placed = a[ 0 ];
for ( int i = 1 ; i < n; i++)
{
int current_cell = a[i];
if (current_cell - last_prisoner_placed >= sep)
{
prisoners_placed++;
last_prisoner_placed = current_cell;
if (prisoners_placed == p)
{
return true ;
}
}
}
return false ;
}
static int maxDistance( int cell[], int n, int p)
{
Arrays.sort(cell);
int start = 0 ;
int end = cell[n - 1 ] - cell[ 0 ];
int ans = 0 ;
while (start <= end)
{
int mid = start + ((end - start) / 2 );
if (canPlace(cell, n, p, mid))
{
ans = mid;
start = mid + 1 ;
}
else
{
end = mid - 1 ;
}
}
return ans;
}
public static void main (String[] args)
{
int cell[] = { 1 , 2 , 8 , 4 , 9 };
int n = cell.length;
int p = 3 ;
System.out.println(maxDistance(cell, n, p));
}
}
|
Python3
def canPlace(a, n, p, sep):
prisoners_placed = 1
last_prisoner_placed = a[ 0 ]
for i in range ( 1 , n):
current_cell = a[i]
if (current_cell - last_prisoner_placed > = sep):
prisoners_placed + = 1
last_prisoner_placed = current_cell
if (prisoners_placed = = p):
return True
return False
def maxDistance(cell, n, p):
cell = sorted (cell)
start = 0
end = cell[n - 1 ] - cell[ 0 ]
ans = 0
while (start < = end):
mid = start + ((end - start) / / 2 )
if (canPlace(cell, n, p, mid)):
ans = mid
start = mid + 1
else :
end = mid - 1
return ans
cell = [ 1 , 2 , 8 , 4 , 9 ]
n = len (cell)
p = 3
print (maxDistance(cell, n, p))
|
C#
using System;
using System.Collections;
class GFG
{
static bool canPlace( int []a, int n,
int p, int sep)
{
int prisoners_placed = 1;
int last_prisoner_placed = a[0];
for ( int i = 1; i < n; i++)
{
int current_cell = a[i];
if (current_cell - last_prisoner_placed >= sep)
{
prisoners_placed++;
last_prisoner_placed = current_cell;
if (prisoners_placed == p)
{
return true ;
}
}
}
return false ;
}
static int maxDistance( int []cell, int n, int p)
{
Array.Sort(cell);
int start = 0;
int end = cell[n - 1] - cell[0];
int ans = 0;
while (start <= end)
{
int mid = start + ((end - start) / 2);
if (canPlace(cell, n, p, mid))
{
ans = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return ans;
}
public static void Main()
{
int []cell = { 1, 2, 8, 4, 9 };
int n = cell.Length;
int p = 3;
Console.WriteLine(maxDistance(cell, n, p));
}
}
|
Javascript
<script>
function canPlace(a , n , p , sep)
{
var prisoners_placed = 1;
var last_prisoner_placed = a[0];
for (i = 1; i < n; i++)
{
var current_cell = a[i];
if (current_cell - last_prisoner_placed >= sep)
{
prisoners_placed++;
last_prisoner_placed = current_cell;
if (prisoners_placed == p)
{
return true ;
}
}
}
return false ;
}
function maxDistance(cell , n , p)
{
cell.sort();
var start = 0;
var end = cell[n - 1] - cell[0];
var ans = 0;
while (start <= end)
{
var mid = start + parseInt(((end - start) / 2));
if (canPlace(cell, n, p, mid))
{
ans = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return ans;
}
var cell = [ 1, 2, 8, 4, 9 ];
var n = cell.length;
var p = 3;
document.write(maxDistance(cell, n, p));
</script>
|
Last Updated :
09 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...