In this Hackerland Radio Transmitters HackerRank solution, 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.
Example
antennae at houses and and provide complete coverage. There is no house at location to cover both and . Ranges of coverage, are , , and .
Hackerland Radio Transmitters HackerRank solution
I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.
Problem Solution in Python
def hackerlandRadioTransmitters(x, k):
x.sort()
count = 0
while x:
leftHouse = x[0]
while x and leftHouse+k >= x[0]:
transmitter=x.pop(0)
while x and transmitter+k >= x[0]:
x.pop(0)
count += 1
return count
Problem Solution in JavaScript
function hackerlandRadioTransmitters(x, k) {
// Sort Array
x.sort((a,b)=>a-b)
let numTransmitters = 0
let coveredPosition = 0
for(let i=0;i<x.length;i++){
// If current house is already covered -> skip
if(x[i]<=coveredPosition) continue
// Find for current i the next house which is outside of reach
let j=i+1
while(((x[i]+k)>=x[j])){
j++
}
// Current x[j] is not in reach of x[i] anymore
// -> Build transmitter on previous house
numTransmitters++
coveredPosition = x[j-1]+k
}
return numTransmitters
}
Problem Solution in C++
int hackerlandRadioTransmitters(vector<int> x, int k) {
sort(x.begin(), x.end());
int count=0;
int i=0;
while(i<x.size())
{
int j=i+1;
while(x[i]+k>=x[j] &&j<x.size())
j++;
int m=j;
j--;
while(x[j]+k>=x[m] && m<x.size())
m++;
i=m;
count++;
}
return count;
}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems
Leave a Reply