Open In App

Check wifi is available to all students or not

Last Updated : 29 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether Wifi is
// available to all or not
bool Wifi_Range(int n, string str, int x)
{
 
    // Count variable
    int count = 0;
    int st = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If str[i] == 0 then
        // increment count
        if (str[i] == '0') {
 
            count++;
        }
        else {
 
            // Check that if count of
            // starting 0 is greater
            // than x then return 0
            if (st == 0 && count > x)
                return 0;
            count = 0;
            st = 1;
        }
 
        // Check that if count of 0 is
        // greater than 2*x then return 0
        if (count > 2 * x)
            return 0;
    }
 
    // Check that if last count 0 is
    // greater than x then return 0
    if (count > x)
        return 0;
 
    // Else return 1
    return 1;
}
 
// Driver code
int main()
{
 
    // Example 1
    string str = "010";
    int n = 3, x = 0;
 
    // Function call
    cout << Wifi_Range(n, str, x) << endl;
 
    // Another input
    str = "10001100";
    n = 9, x = 2;
    cout << Wifi_Range(n, str, x) << endl;
 
    return 0;
}


Java




// Java code for the above approach
public class Main {
    // Function to check whether WiFi is available to all or
    // not
    static boolean wifiRange(int n, String str, int x)
    {
        // Count variable
        int count = 0;
        int st = 0;
 
        for (int i = 0; i < n; i++) {
            // If str[i] == '0' then increment count
            if (str.charAt(i) == '0') {
                count++;
            }
            else {
                // Check if count of starting '0' is greater
                // than x, then return false
                if (st == 0 && count > x)
                    return false;
                count = 0;
                st = 1;
            }
 
            // Check if count of '0' is greater than 2*x,
            // then return false
            if (count > 2 * x)
                return false;
        }
 
        // Check if last count of '0' is greater than x,
        // then return false
        if (count > x)
            return false;
 
        // Otherwise, return true
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Example 1
        String str1 = "010";
        int n1 = 3;
        int x1 = 0;
 
        // Function call
        System.out.println(wifiRange(n1, str1, x1));
 
        // Another input
        String str2 = "10001100";
        int n2 = 8; // Corrected size to match the string
                    // length
        int x2 = 2;
        System.out.println(wifiRange(n2, str2, x2));
    }
}
 
// This code is contributed by shivamgupta0987654321


Python




# Nikunj Sonigara
 
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
 
# Driver code
if __name__ == "__main__":
    # Example 1
    str_1 = "010"
    n_1 = 3
    x_1 = 0
    print(wifi_range(n_1, str_1, x_1))
 
    # Another input
    str_2 = "10001100"
    n_2 = 8
    x_2 = 2
    print(wifi_range(n_2, str_2, x_2))


C#




using System;
 
public class Program
{
   
      // Function to check whether Wifi is
    // available to all or not
    public static int WifiRange(int n, string str, int x)
    {
       
          // Count variable
        int count = 0;
        int st = 0;
        for (int i = 0; i < n; i++)
        {
               
              // If str[i] == 0 then
                // increment count
            if (str[i] == '0')
            {
                count += 1;
            }
            else
            {
               
                  // Check that if count of
                    // starting 0 is greater
                // than x then return 0       
                if (st == 0 && count > x)
                {
                    return 0;
                }
                count = 0;
                st = 1;
            }
           
              // Check that if count of 0 is
            // greater than 2*x then return 0
            if (count > 2 * x)
            {
                return 0;
            }
        }
       
           // Check that if last count 0 is
            // greater than x then return 0
        if (count > x)
        {
            return 0;
        }
       
          // Else return 1
        return 1;
    }
 
      // Driver code
    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




// JavaScript code implementation
 
// function called
function wifiRange(n, str, x) {
 
 // count variable
    let count = 0;
    let st = 0;
 
    for (let i = 0; i < n; i++) {
     
      // If str[i] == 0 then
        // increment count
        if (str[i] === '0') {
            count++;
        }
        else {
         
           // Check that if count of
            // starting 0 is greater
            // than x then return 0
            if (st === 0 && count > x) {
                return 0;
            }
            count = 0;
            st = 1;
        }
 
        // Check that if count of 0 is
        // greater than 2*x then return 0
        if (count > 2 * x) {
            return 0;
        }
    }
     
    // Check that if last count 0 is
    // greater than x then return 0
    if (count > x) {
        return 0;
    }
 
    return 1;
}
 
// Driver code
// Example 1
const str_1 = "010";
const n_1 = 3;
const x_1 = 0;
console.log(wifiRange(n_1, str_1, x_1));
 
// Another input
const str_2 = "10001100";
const n_2 = 8;
const x_2 = 2;
console.log(wifiRange(n_2, str_2, x_2));


Output

0
1

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads