VOOZH about

URL: https://www.geeksforgeeks.org/dsa/hostel-wifi-range-problem/

⇱ Hostel WiFi Range Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hostel WiFi Range Problem

Last Updated : 3 May, 2026

There are n rooms in a straight line in Geekland State University's hostel. You are given a binary string s of length n, where s[i] = '1' means there is a WiFi router in the i-th room, and s[i] = '0' means there is no WiFi in that room.

  • Each WiFi router has a range of x, meaning it can cover up to x rooms to its left and x rooms to its right.
  • Given x and s, find whether all rooms are covered by at least one WiFi router.

Examples:

Input: x = 0, s = "010"
Output: false
Explanation: Since the range is 0, so Wifi is only accessible in second room while 1st & 3rd room have no wifi. Therefore answer is false for this test case.

Input: x = 1, s = "10010"
Output: true
Explanation:
Index 0: WiFi is available.
Index 1: Since the range of the 0th index is 1, WiFi is available here.
Index 2: Since the range of the 3rd index is 1, WiFi is also available here.
Index 3: WiFi is available.
Index 4: The range of the 3rd index covers this position.
So, all the rooms have WiFi. Therefore, the answer is true.

[Naive Approach] One by One Check Rooms - O(n²) Time and O(1) Space

The approach is to check every position in the string and verify whether it is covered by at least one WiFi router.

For each index i, we iterate through all indices j to find a router (s[j] == '1') such that the distance between them is within the allowed range x (i.e., |i - j| ≤ x). If such a router exists, the position is considered covered. If for any position no such router is found, we immediately return false. If all positions are successfully covered, we return true.

[Better Approach] Using Prefix and Suffix Arrays - O(n) Time and O(n) Space

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

Follow the steps to implement the approach:

  • Initialize two arrays left and right of size n with initial values of negative and positive infinity respectively.
  • Iterate i from 0 to n-1. If s[i] is equal to '1', set last equal to i and set left[i] equal to last.
  • Now iterate from n-1 to 0. If s[i] is equal to '1', set last equal to i. Set right[i] equal to last.
  • Iterate through each room i from 0 to n-1.Check if the distance between room i and the nearest wifi in left or right is greater than x.

Output
true

[Expected Approach] Using Greedy Approach - O(n) Time and O(1) Space

We use a greedy approach by keeping track of the farthest position covered so. Whenever we find a WiFi router,, we extend the coverage range. In the end, we verify whether the entire string is covered.

  • Initialize maxReach = -1
  • Traverse the string. If s[i] == '1', check if it able to able to cover all before it. If gap exists, return false
  • Update maxReach = max(maxReach, i + x)
  • After loop, check maxReach >= n - 1

Output
true


Comment