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
Tree: Huffman Decoding
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t
View Solution →Binary Search Tree : Lowest Common Ancestor
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b
View Solution →Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →