Minimum broadcast range required by M towers to reach N houses
Last Updated :
22 Nov, 2021
Given an array a containing positions of N houses, and an array b containing positions of M radio towers, each placed along a horizontal line, the task is to find the minimum broadcast range such that each radio tower reaches every house.
Examples:
Input: a[] = {1, 5, 11, 20}, b[] = {4, 8, 15}
Output: 5
Explanation:
The minimum range required is 5, since that would be required for the tower at position 15 to reach the house at position 20
Input: a[] = {12, 13, 11, 80}, b[] = {4, 6, 15, 60}
Output: 20
Explanation:
The minimum range required is 20, since that would be required for the tower at position 60 to reach the house at position 80
Approach: Traverse both the arrays until the broadcast range for the last house is calculated. For every house, compare its distance from its left and right towers respectively and consider the minimum. Compare this minimum value with the maximum obtained so far and store the maximum.
Note: The distance of the left tower from the first house is considered Integer.MIN_VALUE. If we reach the end of towers, the distance of all remaining houses from the respective right tower is considered Integer.MAX_VALUE.
Below code implements the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int minBroadcastRange( int houses[], int towers[], int n, int m)
{
int leftTower = INT_MIN;
int rightTower = towers[0];
int j = 0, k = 0;
int min_range = 0;
while (j < n) {
if (houses[j] < rightTower) {
int left = houses[j] - leftTower;
int right = rightTower - houses[j];
int local_max = left < right ? left : right;
if (local_max > min_range)
min_range = local_max;
j++;
}
else {
leftTower = towers[k];
if (k < m - 1) {
k++;
rightTower = towers[k];
}
else
rightTower = INT_MAX;
}
}
return min_range;
}
int main()
{
int a[] = { 12, 13, 11, 80 };
int b[] = { 4, 6, 15, 60 };
int n = sizeof (a)/ sizeof (a[0]);
int m = sizeof (b)/ sizeof (b[0]);
int max = minBroadcastRange(a, b,n,m);
cout<<max<<endl;
}
|
Java
import java.io.*;
class GFG {
private static int minBroadcastRange(
int [] houses, int [] towers)
{
int n = houses.length;
int m = towers.length;
int leftTower = Integer.MIN_VALUE;
int rightTower = towers[ 0 ];
int j = 0 , k = 0 ;
int min_range = 0 ;
while (j < n) {
if (houses[j] < rightTower) {
int left = houses[j] - leftTower;
int right = rightTower - houses[j];
int local_max = left < right ? left : right;
if (local_max > min_range)
min_range = local_max;
j++;
}
else {
leftTower = towers[k];
if (k < m - 1 ) {
k++;
rightTower = towers[k];
}
else
rightTower = Integer.MAX_VALUE;
}
}
return min_range;
}
public static void main(String[] args)
{
int [] a = { 12 , 13 , 11 , 80 };
int [] b = { 4 , 6 , 15 , 60 };
int max = minBroadcastRange(a, b);
System.out.println(max);
}
}
|
Python3
import sys
def minBroadcastRange( houses, towers, n, m):
leftTower = - sys.maxsize - 1
rightTower = towers[ 0 ]
j , k = 0 , 0
min_range = 0
while (j < n):
if (houses[j] < rightTower):
left = houses[j] - leftTower
right = rightTower - houses[j]
if left < right :
local_max = left
else :
local_max = right
if (local_max > min_range):
min_range = local_max
j + = 1
else :
leftTower = towers[k]
if (k < m - 1 ) :
k + = 1
rightTower = towers[k]
else :
rightTower = sys.maxsize
return min_range
if __name__ = = "__main__" :
a = [ 12 , 13 , 11 , 80 ]
b = [ 4 , 6 , 15 , 60 ]
n = len (a)
m = len (b)
max = minBroadcastRange(a, b,n,m)
print ( max )
|
C#
using System;
class GFG {
private static int minBroadcastRange(
int [] houses, int [] towers)
{
int n = houses.Length;
int m = towers.Length;
int leftTower = int .MinValue;
int rightTower = towers[0];
int j = 0, k = 0;
int min_range = 0;
while (j < n) {
if (houses[j] < rightTower) {
int left = houses[j] - leftTower;
int right = rightTower - houses[j];
int local_max = left < right ? left : right;
if (local_max > min_range)
min_range = local_max;
j++;
}
else {
leftTower = towers[k];
if (k < m - 1) {
k++;
rightTower = towers[k];
}
else
rightTower = int .MaxValue;
}
}
return min_range;
}
public static void Main(String[] args)
{
int [] a = { 12, 13, 11, 80 };
int [] b = { 4, 6, 15, 60 };
int max = minBroadcastRange(a, b);
Console.WriteLine(max);
}
}
|
Javascript
<script>
function minBroadcastRange(
houses, towers)
{
let n = houses.length;
let m = towers.length;
let leftTower = Number.MIN_VALUE;
let rightTower = towers[0];
let j = 0, k = 0;
let min_range = 0;
while (j < n) {
if (houses[j] < rightTower) {
let left = houses[j] - leftTower;
let right = rightTower - houses[j];
let local_max = left < right ? left : right;
if (local_max > min_range)
min_range = local_max;
j++;
}
else {
leftTower = towers[k];
if (k < m - 1) {
k++;
rightTower = towers[k];
}
else
rightTower = Number.MAX_VALUE;
}
}
return min_range;
}
let a = [12, 13, 11, 80 ];
let b = [ 4, 6, 15, 60 ];
let max = minBroadcastRange(a, b);
document.write(max);
</script>
|
Time complexity: O(M + N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...