Hackerland Radio Transmitters


Problem Statement :


Hackerland is a one-dimensional city with houses aligned at integral locations along a road. The Mayor wants to install radio transmitters on the roofs of the city's houses. Each transmitter has a fixed range meaning it can transmit a signal to all houses within that number of units distance away.

Given a map of Hackerland and the transmission range, determine the minimum number of transmitters so that every house is within range of at least one transmitter. Each transmitter must be installed on top of an existing house.


Function Description

Complete the hackerlandRadioTransmitters function in the editor below.

hackerlandRadioTransmitters has the following parameter(s):

int x[n]: the locations of houses
int k: the effective range of a transmitter
Returns

int: the minimum number of transmitters to install

Input Format

The first line contains two space-separated integers n and k, the number of houses in Hackerland and the range of each transmitter.
The second line contains n space-separated integers describing the respective locations of each house 
x[i] .


Output Format

Print a single integer denoting the minimum number of transmitters needed to cover all of the houses.



Solution :



title-img


                            Solution in C :

In  C++  :







#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main(){
	int n, k;
	cin >> n >> k;
	int x[n];
	for(int i = 0; i < n; i++) scanf("%d", &x[i]);
	sort(x,x+n);
	int ans = 0;
	int cur = 0;
	while(cur < n){
		int st = x[cur];
		while(cur < n && x[cur+1] <= st+k){
			cur++;
		}
		int m = x[cur];
		while(cur < n && x[cur+1] <= m+k){
			cur++;
		}
		ans++;
		cur++;
	}
	cout << ans << endl;
}









In   Java  :







import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] x = new int[n];
        for(int x_i=0; x_i < n; x_i++){
            x[x_i] = in.nextInt();
        }
        
        Arrays.sort(x);
        
        int left = 0,right,mid, ans = 0;
        int end;
        
        while(left < n) {
            right = left;
            mid = left;
            ans++;
            
            while(mid < n && x[mid] - x[left] <= k) {
                mid++; // mid will be out
            }
            mid--;
            end = x[mid] + k;
            right = mid + 1;
            
            while(right < n && x[right] <= end) {
                right++;
            }
            left = right;
        }
        
        System.out.println(ans);
    }
}









In   C  :






#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}
int main(){
    int n; 
    int k,count=0,i,j,x_i; 
    scanf("%d %d",&n,&k);
    int *x = malloc(sizeof(int) * 100005);
    for(i=0;i<100005;i++)
        x[i]=0;
    for(i = 0; i < n; i++){
       scanf("%d",&x_i);
       x[x_i]=1;
    }
    for(i=0;i<100005;i++)
    {
        if(x[i]==1)
        {
            j=i+k;
            if(j>=100005)
                j=100004;
            for(;j>=i;j--)
            {
                if(x[j]==1)
                {
                    count++;
                    i=j+k;
                    break;
                }
            }
        }
    }
    printf("%d",count);
    return 0;
}









In  Python3 :






n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
x = [int(x_temp) for x_temp in input().strip().split(' ')]
x.sort()
count=0
length=len(x)
i=0
while i<length:
    temp=x[i]
    while length>i and x[i]-temp<=k:
        i+=1
    head=x[i-1]
    while length>i and x[i]-head<=k:
        i+=1
    count+=1
print(count)
                        








View More Similar Problems

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →

Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

View Solution →

Heavy Light 2 White Falcon

White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with N nodes and each node's value Vi is initially 0. Let's denote the path fr

View Solution →

Library Query

A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. Now, being the geek that you are, you thought of the following two queries which can be performed on these shelves. Change the number of books in one of the shelves. Obtain the number of books on the shelf having the kth rank within the ra

View Solution →