Find maximum distance between any city and 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
function findMaxDistance ( numOfCities , station , n ) { var hasStation = Array ( numOfCities + 1 ) . fill ( false ) ; for ( var city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } var dist = 0 ; var maxDist = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { maxDist = Math . min ( station [ i ] , maxDist ) ; } for ( var 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 ) ; } var numOfCities = 6 ; var station = [ 3 , 1 ] ; var n = station . length ; document . write ( " " + findMaxDistance ( numOfCities , station , n ) ) ;