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

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →