In this article, we are going to solve a hacker rank problem called Hackerland Radio Transmitter.
HackerRank is a coding-based online platform where programmers, developers, and hackers can learn about, practice, and solve competitive programming problems, tests, and challenges. Hackerland Radio Transmitter is an example and you can find its problem statement here.
Imagine you are in a one-dimensional city where houses are inline, and you need to install transmitters on the roofs. The transmitter has a fixed range where it can only cover houses within a specified area. The goal is to print the minimum number of transmitters needed to cover all the houses. A transmitter can only be installed on an existing house.
We are given an array of integer,
X, describing the location of each house and
K for the range of a transmitter. Here is an example:
K = 1. X = 1 2 3 4 5
Xby ascending order. This will also prevent duplicate values on the code later on.
installedvariable will refer to the number of installed transmitters. This is the variable we are going to return in our function.
midvariable will help us to track the middle distance of transmitter.
endvariable will help us to track the farthest distance of transmitter.
x[i] less than or equal to midand we increment
ivariable upon loop.
x[i]is in the middle position, we increment the
installedvariable by 1.
Then we will do the same to find the farthest distance.
This will keep looping until it covers all the houses.
Here’s the full code: