Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Hackerland Radio Transmitters Problem Solution

HackerRank Hackerland Radio Transmitters

Posted on May 6, 2023May 6, 2023 By Yashwant Parihar No Comments on HackerRank Hackerland Radio Transmitters

In this post, we will solve HackerRank Hackerland Radio Transmitters.

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
x = [1, 2, 3, 5, 9]
k = 1
3 antennae at houses 2 and 5 and 9 provide complete coverage. There is no house at location 7 to cover both 5 and 9. Ranges of coverage, are [1, 2, 3], [5], and [9].

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 [i].

Sample Input 0

STDIN       Function
-----       --------
5 1         x[] size n = 5, k = 1
1 2 3 4 5   x = [1, 2, 3, 4, 5]  

Sample Output 0

2
HackerRank Hackerland Radio Transmitters Problem Solution
HackerRank Hackerland Radio Transmitters Problem Solution

Table of Contents

  • Hackerland Radio Transmitters C Solution
  • Hackerland Radio Transmitters C++ Solution
  • Hackerland Radio Transmitters C Sharp Solution
  • Hackerland Radio Transmitters Java Solution
  • Hackerland Radio Transmitters JavaScript Solution
  • Hackerland Radio Transmitters Python Solution

Hackerland Radio Transmitters C Solution

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


void quickSort( int a[], int l, int r);

int main(){
    int n; 
    int k; 
    scanf("%d %d",&n,&k);
    int *x = malloc(sizeof(int) * n);
    for(int x_i = 0; x_i < n; x_i++){
       scanf("%d",&x[x_i]);
    }
    quickSort(x,0,n-1);
    int count = 0;
    for(int x_i = 0; x_i < n; x_i++){
       int start = x[x_i];
        int trans = 0;
        int group = 0;
       while(group ==0){
           if(x_i >= n){
               break;
           }
           if(start+k >= x[x_i]){
               trans = x_i;
               x_i++;
           }
           else if(x[trans]+k >= x[x_i] ){
              x_i++;
           }
           else{
               group = 1;
               x_i--;
           }
       }
        count++;
    }
    printf("%d",count);
    return 0;
}




int partition( int a[], int l, int r) {
   int pivot, i, j, t;
   pivot = a[l];
   i = l; j = r+1;
		
   while( 1){
   	do ++i; while( a[i] <= pivot && i <= r );
   	do --j; while( a[j] > pivot );
   	if( i >= j ) break;
   	t = a[i]; a[i] = a[j]; a[j] = t;
   }
   t = a[l]; a[l] = a[j]; a[j] = t;
   return j;
}

void quickSort( int a[], int l, int r)
{
   int j;

   if( l < r ){
        j = partition( a, l, r);
       quickSort( a, l, j-1);
       quickSort( a, j+1, r);
   }
	
}

Hackerland Radio Transmitters C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define DN 100005
using namespace std;


int a[DN];
int n;

int find_new_location(int ind, int k) {
    for (int i = ind; i < n; ++i) {
        if (a[i] - a[ind] > k)
            return i-1;
    }
    return n-1;
}

int main() {
    int k, t = 1, loc;
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a, a+n);
    loc = find_new_location(0, k);
    for (int i = 1; i < n; ++i)
        if (abs(a[i] - a[loc]) > k) {
            ++t;
            loc = find_new_location(i, k);
            i = loc;
        }
    cout << t;
    return 0;
}

Hackerland Radio Transmitters C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        string[] tokens_n = Console.ReadLine().Split(' ');
        int n = Convert.ToInt32(tokens_n[0]);
        int k = Convert.ToInt32(tokens_n[1]);
        string[] x_temp = Console.ReadLine().Split(' ');
        int[] x = Array.ConvertAll(x_temp,Int32.Parse);
        int count = 0;

            Array.Sort(x);

            int transmiterPos = 0;
            int start = -1; 
            foreach(int item in x)
            {
                if (start < 0)
                {
                    start = item;
                    count++;
                    transmiterPos = item; 
                }

                int range = item - start;
                transmiterPos = (range <= k) ? item : transmiterPos;

                if(item - transmiterPos > k)
                {
                    start = item;
                    transmiterPos = item; 
                    count++; 
                }
            }
        Console.WriteLine(count);
        
    }
}

Hackerland Radio Transmitters Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'hackerlandRadioTransmitters' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY x
     *  2. INTEGER k
     */

    public static int hackerlandRadioTransmitters(List<Integer> list, int k) {
    // Write your code here
    
    Collections.sort(list);
    list.add(0);
    int count=0;
    int n=list.size();
    int i=0;
    while(i<n)
    {
        int j=i;
        while( j<n && list.get(j)-list.get(i)<=k)
        j++;
        j--;
        count++;
        
        i=j+1;
        while(i<n && list.get(i)-list.get(j)<=k)
        i++;
         
         
    }
    
    return count;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(firstMultipleInput[0]);

        int k = Integer.parseInt(firstMultipleInput[1]);

        List<Integer> x = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.hackerlandRadioTransmitters(x, k);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Hackerland Radio Transmitters JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function subarr (main, sub) {
  var result = sub.filter(function(item) {
    return main.indexOf(item) > -1;
  });
  return sub.length === result.length;
}

function main() {
    var n_temp = readLine().split(' ');
    var n = parseInt(n_temp[0]);
    var k = parseInt(n_temp[1]);
    var x = readLine().split(' ');
    x = x.map(Number).sort((a, b) => (a - b));
    let current;
    let result = 0;
    let i = 0;

    while (i < n) {
        current = x[i] + k;
        while (i < n && x[i] <= current) {
            i++;
        }
        current = x[--i] + k;
        while (i < n && x[i] <= current) {
            i++;
        } 
        result++;
    }
    console.log(result);
}

Hackerland Radio Transmitters Python Solution

#!/bin/python3
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
x = [int(x_temp) for x_temp in input().strip().split(' ')]
v= 2*k
x= list(set(x))
x.sort()
total = 1 
left = x[0]
middle = x[0]
l = 1
while l<= len(x)-1:
    if x[l] - left > v:
        left = x[l]
        middle = x[l]
        total = total+1
    else:
        if x[l] -left <= k :
            middle = x[l]
        else:
            if x[l]-middle > k:
                total =total +1
                left = x[l]
                middle = x[l]
    l =l+1        
print(total)
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank How Many Substrings? Solution
Next Post: HackerRank Gridland Metro Problem Solution

Related Posts

HackerRank Two Strings Problem Solution HackerRank Two Strings Problem Solution c
HackerRank Funny String Problem Solution HackerRank Funny String Problem Solution c
HackerRank Cut the sticks Problem Solution HackerRank Cut the sticks Problem Solution c
HackerRank Cats and a Mouse Problem Solution HackerRank Cats and a Mouse Problem Solution c
HackerRank Subarray Division Problem Solution HackerRank Subarray Division Problem Solution c
HackerRank Climbing the Leaderboard Problem Solution HackerRank Climbing the Leaderboard Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme