Check wifi is available to all students or not
Last Updated :
29 Sep, 2023
Given a binary string S of length N, where S[i] = ‘1‘ represents the presence of a wifi in the ith room and S[i] = ‘0’ represents no wifi. Each wifi has range X i.e. if there is a wifi in the ith room then its range will go up to X more rooms on its left as well as right. Find whether students in all rooms can use wifi or not.
Examples:
Input: N = 3, X = 0, S = “010”
Output: 0
Explanation: Since the range(X)=0, So Wifi is only accessible in the second room & 1st & the 2nd rooms have no wifi.
Input: N = 5, X = 1, S = “10010”
Output: 1
Explanation:
- Index 0: Wifi is available
- Index 1: Since the range of the 0th Index is 1, so, here wifi will be available.
- Index 2: Since the range of the 3rd Index is 1, so, there is also wifi available.
- Index 3: Wifi is available
- Index 4: here range of the 3rd Index is available.
So all the rooms have wifi, so return true.
Approach: This can be solved with the following idea:
Count the number of consecutive ‘0‘ in the string. If the count is greater than the double of the given range then return 0. Also check the count of ‘0’ starting and ending of the string is greater than the given range then return 0 else return 1.
Below are given steps to solve the problem:
- Take a variable count to count consecutive 0 and st variables to check the first 1 in the string.
- If the first 1 occurred, check the length of a sequence of zeros is greater than x, and return 0.
- If the count variable is more than 2*x zeros, it returns false.
- Finally, the count has a count of the last 0’s. If it is greater than x then return 0 else returns 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool Wifi_Range( int n, string str, int x)
{
int count = 0;
int st = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' ) {
count++;
}
else {
if (st == 0 && count > x)
return 0;
count = 0;
st = 1;
}
if (count > 2 * x)
return 0;
}
if (count > x)
return 0;
return 1;
}
int main()
{
string str = "010" ;
int n = 3, x = 0;
cout << Wifi_Range(n, str, x) << endl;
str = "10001100" ;
n = 9, x = 2;
cout << Wifi_Range(n, str, x) << endl;
return 0;
}
|
Java
public class Main {
static boolean wifiRange( int n, String str, int x)
{
int count = 0 ;
int st = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '0' ) {
count++;
}
else {
if (st == 0 && count > x)
return false ;
count = 0 ;
st = 1 ;
}
if (count > 2 * x)
return false ;
}
if (count > x)
return false ;
return true ;
}
public static void main(String[] args)
{
String str1 = "010" ;
int n1 = 3 ;
int x1 = 0 ;
System.out.println(wifiRange(n1, str1, x1));
String str2 = "10001100" ;
int n2 = 8 ;
int x2 = 2 ;
System.out.println(wifiRange(n2, str2, x2));
}
}
|
Python
def wifi_range(n, str , x):
count = 0
st = 0
for i in range (n):
if str [i] = = '0' :
count + = 1
else :
if st = = 0 and count > x:
return 0
count = 0
st = 1
if count > 2 * x:
return 0
if count > x:
return 0
return 1
if __name__ = = "__main__" :
str_1 = "010"
n_1 = 3
x_1 = 0
print (wifi_range(n_1, str_1, x_1))
str_2 = "10001100"
n_2 = 8
x_2 = 2
print (wifi_range(n_2, str_2, x_2))
|
C#
using System;
public class Program
{
public static int WifiRange( int n, string str, int x)
{
int count = 0;
int st = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
{
count += 1;
}
else
{
if (st == 0 && count > x)
{
return 0;
}
count = 0;
st = 1;
}
if (count > 2 * x)
{
return 0;
}
}
if (count > x)
{
return 0;
}
return 1;
}
public static void Main( string [] args)
{
string str_1 = "010" ;
int n_1 = 3;
int x_1 = 0;
Console.WriteLine(WifiRange(n_1, str_1, x_1));
string str_2 = "10001100" ;
int n_2 = 8;
int x_2 = 2;
Console.WriteLine(WifiRange(n_2, str_2, x_2));
}
}
|
Javascript
function wifiRange(n, str, x) {
let count = 0;
let st = 0;
for (let i = 0; i < n; i++) {
if (str[i] === '0' ) {
count++;
}
else {
if (st === 0 && count > x) {
return 0;
}
count = 0;
st = 1;
}
if (count > 2 * x) {
return 0;
}
}
if (count > x) {
return 0;
}
return 1;
}
const str_1 = "010" ;
const n_1 = 3;
const x_1 = 0;
console.log(wifiRange(n_1, str_1, x_1));
const str_2 = "10001100" ;
const n_2 = 8;
const x_2 = 2;
console.log(wifiRange(n_2, str_2, x_2));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...