- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find maximum distance between any city and station in C++

## Concept

With respect of the given number of cities N numbered from 0 to N-1 and the cities in which stations are located, our task is to determine the maximum distance between any city and its nearest station. It should be noted that the cities with stations can be given in any order.

## Input

numOfCities = 6, stations = [2, 4]

## Output

2

## Input

numOfCities = 6, stations = [4]

## Output

4

The following figure indicates the first example containing 6 cities and the cities with stations highlighted with green color. So, in this case, the farthestcities from its nearest stations are 0 at a distance of 2. Hence maximum distance is 1.

In the second example, the farthest city from its nearest station is 0 which is at a distance of 4. Hence maximum distance is 4.

## Method

Here, there are three possible cases in this problem −

First case indicates when the farthest city is between two stations.

Second case indicates when the farthest city is on the left side of the first station.

Last case indicates when the farthest city is on the right side of the last station.

Following algorithm is implemented to solve the above problem −

We initialize a boolean array of size N (number of cities) with False. After that mark the values of cities with stations as True

Next we initialize a variable dist with 0. We have to initialize another variablemaxDist with value which is same to the first city with station (used for second case).

Begin looping through all the cities ony by one.

It has been observed that if the current city has a station, and then assign the maximum of (dist+1)//2 and maxDist to maxDist (used for first case). In addition,assign 0 to dist.

Else, increment dist.

Finally, return the maximum of dist and maxDist (used for third case).

## Example

// C++ program to calculate the maximum // distance between any city // and its nearest station #include<bits/stdc++.h> using namespace std; // Shows function to compute the maximum // distance between any city and its nearest station int findMaxDistance(int numOfCities1,int station1[],int N){ // Used to initialize boolean list bool hasStation[numOfCities1 + 1] = {false}; // Used to assign True to cities containing station for (int city1 = 0; city1 < N; city1++){ hasStation[station1[city1]] = true; } int dist1 = 0; int maxDist1 = INT_MAX; for(int i = 0; i < N; i++){ maxDist1 = min(station1[i],maxDist1); } for (int city1 = 0; city1 < numOfCities1; city1++){ if (hasStation[city1] == true){ maxDist1 = max((dist1 + 1) / 2, maxDist1); dist1 = 0; } else dist1 += 1; } return max(maxDist1, dist1); } //Driver code int main(){ int numOfCities1 = 6; int station1[] = {2,4}; int N = sizeof(station1)/sizeof(station1[0]); cout << "Max Distance:" << findMaxDistance(numOfCities1, station1, N); }

## Output

Max Distance:2

- Related Questions & Answers
- Find maximum distance between any city and station in Python
- Minimize Max Distance to Gas Station in C++
- Program to find maximum distance between empty and occupied seats in Python
- Find the shortest distance between any pair of two different good nodes in C++
- C++ program to find maximum distance between two rival students after x swaps
- Program to check we can visit any city from any city or not in Python
- Total Distance to Visit City Blocks in Python
- Find the City With the Smallest Number of Neighbors at a Threshold Distance in C++
- Find current weather of any city using OpenWeatherMap API in Python
- Program to find maximum distance between a pair of values in Python
- Find the minimum distance between two numbers in C++
- Maximum distance between two occurrences of same element in array in C
- Gas Station in C++
- Distance between Vertices and Eccentricity
- C++ Program to Find Maximum Value of any Algebraic Expression