Hostel WiFi Range Problem
Last Updated :
28 Jan, 2024
Geekland State University’s hostel has a line arrangement of N rooms. You have been provided with a binary string S of length N. In this string, if S[i] = ‘1’, it means that the ith room has wifi; if S[i] = ‘0’, it means there is no wifiS[i] = ‘1’ it means that the ith room has wifi while if S[i] in that room. Each wifi has a range denoted by X, which implies that if there is a wifi in the ith room its range will extend to X rooms on both sides. Your task is to determine whether all the students, in these rooms can access the wifi.
Examples:
Input: N = 5, X = 1, S = “10010”
Output: true
Explanation: WiFi is available in all rooms. The WiFi in room 0 covers room 1, and the WiFi in room 3 covers room 2 and room 4, so all rooms have WiFi.
Input: N = 7, X = 2, S = “1000010”
Output: true
Explanation: WiFi is available in all rooms. The WiFi in room 0 covers rooms 1 and 2. The WiFi in room 5 covers rooms 3, 4, 5, and 6, so all rooms have WiFi.
Approach: To solve the problem follow the below idea.
We can use prefix array and suffix array to preprocess the positions of the nearest wifi in the left and right directions for each room. This allows us to efficiently check if there is at least one wifi that covers each room, by comparing the distances between the room and the nearest wifi in both directions.
If the distance is greater than the wifi range X, we can return false as not all students can use wifi. If the loop completes without returning false, we can return true as all students can use wifi.
Follow the steps to implement the approach:
- Initialize two arrays A and B of size N with initial values of negative and positive infinity respectively.
- Iterate through each room i from 0 to N-1.
- If S[i] is equal to ‘1‘, set cur equal to i.
- set A[i] equal to cur.
- Now once again iterate through each room i from N-1 to 0.
- If S[i] is equal to ‘1‘, set cur equal to i.
- Set B[i] equal to cur.
- Iterate through each room i from 0 to N-1.Check if the distance between room i and the nearest wifi in A or B is greater than X.
- If so, return false as not all students can use wifi.
- If the loop in step 5 completes and all distances are less than or equal to X, return true as all students can use wifi.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
bool wifiRange( int N, string S, int X)
{
vector< int > A(N, -1e9), B(N, 1e9);
int cur = -1e9;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' ) {
cur = i;
}
A[i] = cur;
}
cur = 1e9;
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == '1' ) {
cur = i;
}
B[i] = cur;
}
for ( int i = 0; i < N; i++) {
if ( abs (i - A[i]) > X && abs (i - B[i]) > X) {
return false ;
}
}
return true ;
}
int main()
{
int N1 = 5;
int X1 = 1;
string S1 = "10010" ;
bool result1 = wifiRange(N1, S1, X1);
cout << (result1 ? "true" : "false" ) << endl;
int N2 = 6;
int X2 = 1;
string S2 = "100010" ;
bool result2 = wifiRange(N2, S2, X2);
cout << (result2 ? "true" : "false" ) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static boolean wifiRange( int N, String S, int X) {
int [] A = new int [N];
int [] B = new int [N];
for ( int i = 0 ; i < N; i++) {
A[i] = - 1000000000 ;
B[i] = 1000000000 ;
}
int cur = - 1000000000 ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == '1' ) {
cur = i;
}
A[i] = cur;
}
cur = 1000000000 ;
for ( int i = N - 1 ; i >= 0 ; i--) {
if (S.charAt(i) == '1' ) {
cur = i;
}
B[i] = cur;
}
for ( int i = 0 ; i < N; i++) {
if (Math.abs(i - A[i]) > X && Math.abs(i - B[i]) > X) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int N1 = 5 ;
int X1 = 1 ;
String S1 = "10010" ;
boolean result1 = wifiRange(N1, S1, X1);
System.out.println(result1 ? "true" : "false" );
int N2 = 6 ;
int X2 = 1 ;
String S2 = "100010" ;
boolean result2 = wifiRange(N2, S2, X2);
System.out.println(result2 ? "true" : "false" );
}
}
|
Python3
def wifi_range(N, S, X):
A = [ - 1e9 ] * N
B = [ 1e9 ] * N
cur = - 1e9
for i in range (N):
if S[i] = = '1' :
cur = i
A[i] = cur
cur = 1e9
for i in range (N - 1 , - 1 , - 1 ):
if S[i] = = '1' :
cur = i
B[i] = cur
for i in range (N):
if abs (i - A[i]) > X and abs (i - B[i]) > X:
return False
return True
N1, X1, S1 = 5 , 1 , "10010"
result1 = wifi_range(N1, S1, X1)
print ( "true" if result1 else "false" )
N2, X2, S2 = 6 , 1 , "100010"
result2 = wifi_range(N2, S2, X2)
print ( "true" if result2 else "false" )
|
C#
using System;
public class Program
{
public static bool WifiRange( int N, string S, int X)
{
int [] A = new int [N];
int [] B = new int [N];
Array.Fill(A, -1000000000);
Array.Fill(B, 1000000000);
int cur = -1000000000;
for ( int i = 0; i < N; i++)
{
if (S[i] == '1' )
{
cur = i;
}
A[i] = cur;
}
cur = 1000000000;
for ( int i = N - 1; i >= 0; i--)
{
if (S[i] == '1' )
{
cur = i;
}
B[i] = cur;
}
for ( int i = 0; i < N; i++)
{
if (Math.Abs(i - A[i]) > X && Math.Abs(i - B[i]) > X)
{
return false ;
}
}
return true ;
}
public static void Main()
{
int N1 = 5;
int X1 = 1;
string S1 = "10010" ;
bool result1 = WifiRange(N1, S1, X1);
Console.WriteLine(result1 ? "true" : "false" );
int N2 = 6;
int X2 = 1;
string S2 = "100010" ;
bool result2 = WifiRange(N2, S2, X2);
Console.WriteLine(result2 ? "true" : "false" );
}
}
|
Javascript
function wifiRange(N, S, X) {
let A = new Array(N).fill(-1e9);
let B = new Array(N).fill(1e9);
let cur = -1e9;
for (let i = 0; i < N; i++) {
if (S[i] === '1' ) {
cur = i;
}
A[i] = cur;
}
cur = 1e9;
for (let i = N - 1; i >= 0; i--) {
if (S[i] === '1' ) {
cur = i;
}
B[i] = cur;
}
for (let i = 0; i < N; i++) {
if (Math.abs(i - A[i]) > X && Math.abs(i - B[i]) > X) {
return false ;
}
}
return true ;
}
let N1 = 5;
let X1 = 1;
let S1 = "10010" ;
let result1 = wifiRange(N1, S1, X1);
console.log(result1 ? "true" : "false" );
let N2 = 6;
let X2 = 1;
let S2 = "100010" ;
let result2 = wifiRange(N2, S2, X2);
console.log(result2 ? "true" : "false" );
|
Time Complexity:– O(N), as we loop through each room only three times.
Auxiliary space:– O(N), as we use two arrays of size N to store the leftmost and rightmost wifi indices.
Share your thoughts in the comments
Please Login to comment...