Given the number of cities n numbered from 0 to n-1 and the cities in which stations are located, the task is to find the maximum distance between any city and its nearest station. Note that the cities with stations can be given in any order.
Input: numOfCities = 6, stations = [1, 4] Output: 1 Input: numOfCities = 6, stations =  Output: 3 Input: numOfCities = 6, stations = [3, 1] Output: 2
- The below figure represents the first example containing 6 cities and the cities with stations highlighted with green color. In this case, the farthest cities from their nearest stations are 0, 2, 3 and 5 each at a distance of 1. Hence maximum distance is 1.
In the second example, the farthest city from its nearest station is 0 which is at a distance of 3. Hence maximum distance is 3.
In the third example, the farthest city from its nearest station is 5 which is at a distance of 2. Hence maximum distance is 2.
Approach: There are three possible cases in this problem:
- When the farthest city is between two stations.
- When the farthest city is on the left side of the first station.
- When the farthest city is on the right side of the last station.
Below is the algorithm to solve the above problem:
- Initialize a boolean array of size n (number of cities) with False. Then mark the values of cities with stations as True
- Initialize a variable dist with 0. Initialize another variable maxDist with value which is equal to the first city with station (used for case 2).
- Start looping through all the cities ony by one.
- If the current city has a station, then assign the maximum of (dist+1)//2 and maxDist to maxDist (used for case 1). Also, assign 0 to dist.
- Else, increment dist.
- At the end, return the maximum of dist and maxDist (used for case 3).
Below is the implementation of the above approach:
Max Distance: 2
Time Complexity: O(n)
Space Complexity: O(n)
- Find a rotation with maximum hamming distance
- Find the maximum distance covered using n bikes
- Pair with given sum and maximum shortest distance from end
- Maximum distance between two occurrences of same element in array
- Find points at a given distance on a line of given slope
- Count paths with distance equal to Manhattan distance
- Haversine formula to find distance between two points on a sphere
- Queries to find distance between two nodes of a Binary tree
- Find the distance covered to collect items at equal distances
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Number of stopping station problem
- Minimum Number of Platforms Required for a Railway/Bus Station
- Minimum total cost incurred to reach the last station
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29