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
< ? php function findMaxDistance ( $ numOfCities , $ station , $ n ) { $ hasStation = array_fill ( 0 , $ numOfCities + 1 , false ) ; for ( $ city = 0 ; $ city < $ n ; $ city ++ ) { $ hasStation [ $ station [ $ city ] ] = true ; } $ dist = 0 ; $ maxDist = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ maxDist = min ( $ station [ $ i ] , $ maxDist ) ; } for ( $ city = 0 ; $ city < $ numOfCities ; $ city ++ ) { if ( $ hasStation [ $ city ] == true ) { $ maxDist = max ( ( int ) ( ( $ dist + 1 ) / 2 ) , $ maxDist ) ; $ dist = 0 ; } else $ dist += 1 ; } return max ( $ maxDist , $ dist ) ; } $ numOfCities = 6 ; $ station = array ( 3 , 1 ) ; $ n = count ( $ station ) ; echo " Max ▁ Distance : ▁ " . findMaxDistance ( $ numOfCities , $ station , $ n ) ; ? >