Find maximum distance between any city and station | C # program to calculate the maximum distance between any city and its nearest station ; Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
using System ; class GFG { static int findMaxDistance ( int numOfCities , int [ ] station , int n ) { bool [ ] hasStation = new bool [ numOfCities + 1 ] ; for ( int city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } int dist = 0 ; int maxDist = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { maxDist = Math . Min ( station [ i ] , maxDist ) ; } for ( int city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = Math . Max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return Math . Max ( maxDist , dist ) ; } public static void Main ( String [ ] args ) { int numOfCities = 6 ; int [ ] station = { 3 , 1 } ; int n = station . Length ; Console . WriteLine ( " Max ▁ Distance : " + findMaxDistance ( numOfCities , station , n ) ) ; } }