**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 :

` ````
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

## Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →## Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →## Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →