Skip to content
Related Articles

Related Articles

Improve Article
Minimum broadcast range required by M towers to reach N houses
  • Difficulty Level : Hard
  • Last Updated : 29 Apr, 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:
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++




// CPP program to implement the above approach
#include<bits/stdc++.h>
using namespace std;
 
int minBroadcastRange(int houses[], int towers[],int n,int m)
    {
        // Initialize distance of left
        // tower from first house
        int leftTower = INT_MIN;
 
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
 
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
 
        // Store the minimum required range
        int min_range = 0;
 
        while (j < n) {
 
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
 
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
 
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
 
                if (local_max > min_range)
 
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
 
                // updating the left tower
                leftTower = towers[k];
 
                if (k < m - 1) {
 
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = INT_MAX;
            }
        }
        return min_range;
    }
 
    // Driver code
    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;
    }
 
// This code is contributed by Surendra_Gangwar

Java




// Java program to implement the above approach
 
import java.io.*;
 
class GFG {
 
    private static int minBroadcastRange(
        int[] houses, int[] towers)
    {
 
        // Store no of houses
        int n = houses.length;
 
        // Store no of towers
        int m = towers.length;
 
        // Initialize distance of left
        // tower from first house
        int leftTower = Integer.MIN_VALUE;
 
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
 
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
 
        // Store the minimum required range
        int min_range = 0;
 
        while (j < n) {
 
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
 
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
 
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
 
                if (local_max > min_range)
 
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
 
                // updating the left tower
                leftTower = towers[k];
 
                if (k < m - 1) {
 
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = Integer.MAX_VALUE;
            }
        }
        return min_range;
    }
 
    // Driver code
    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




# Python 3 program to implement the above approach
import sys
 
def minBroadcastRange( houses, towers, n, m):
 
    # Initialize distance of left
    # tower from first house
    leftTower = -sys.maxsize - 1
 
    # Initialize distance of right
    # tower from first house
    rightTower = towers[0]
 
    # j: Index of houses[]
    # k: Index of towers[]
    j , k = 0 , 0
 
    # Store the minimum required range
    min_range = 0
 
    while (j < n):
 
        # If the house lies between
        # left and right towers
        if (houses[j] < rightTower):
 
            left = houses[j] - leftTower
            right = rightTower - houses[j]
 
            # Compare the distance between the
            # left and right nearest towers
            if left < right :
                local_max = left
            else:
                local_max = right
 
            if (local_max > min_range):
 
                # updating the maximum value
                min_range = local_max
            j += 1
         
        else:
 
            # updating the left tower
            leftTower = towers[k]
 
            if (k < m - 1) :
 
                k += 1
 
                # updating the right tower
                rightTower = towers[k]
             
            else:
                # updating right tower
                # to maximum value after
                # reaching the end of Tower array
                rightTower = sys.maxsize
    return min_range
 
# Driver code
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)
 
# This code is contributed by chitranayal

C#




// C# program to implement the above approach
 using System;
 
class GFG {
  
    private static int minBroadcastRange(
        int[] houses, int[] towers)
    {
  
        // Store no of houses
        int n = houses.Length;
  
        // Store no of towers
        int m = towers.Length;
  
        // Initialize distance of left
        // tower from first house
        int leftTower = int.MinValue;
  
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
  
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
  
        // Store the minimum required range
        int min_range = 0;
  
        while (j < n) {
  
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
  
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
  
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
  
                if (local_max > min_range)
  
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
  
                // updating the left tower
                leftTower = towers[k];
  
                if (k < m - 1) {
  
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = int.MaxValue;
            }
        }
        return min_range;
    }
  
    // Driver code
    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);
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript




<script>
 
// Javascript program to implement
// the above approach
 
function minBroadcastRange(
       houses, towers)
    {
   
        // Store no of houses
        let n = houses.length;
   
        // Store no of towers
        let m = towers.length;
   
        // Initialize distance of left
        // tower from first house
        let leftTower = Number.MIN_VALUE;
   
        // Initialize distance of right
        // tower from first house
        let rightTower = towers[0];
   
        // j: Index of houses[]
        // k: Index of towers[]
        let j = 0, k = 0;
   
        // Store the minimum required range
        let min_range = 0;
   
        while (j < n) {
   
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
   
                let left = houses[j] - leftTower;
                let right = rightTower - houses[j];
   
                // Compare the distance between the
                // left and right nearest towers
                let local_max = left < right ? left : right;
   
                if (local_max > min_range)
   
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
   
                // updating the left tower
                leftTower = towers[k];
   
                if (k < m - 1) {
   
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = Number.MAX_VALUE;
            }
        }
        return min_range;
    }
 
// Driver Code
 
        let a = [12, 13, 11, 80 ];
        let b = [ 4, 6, 15, 60 ];
        let max = minBroadcastRange(a, b);
        document.write(max);
 
</script>
Output: 



20

 

Time complexity: O(M + N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :