• + 0 comments

    Java 8 solution:

    static int flatlandSpaceStations(int n, int[] c) {
            // Sort space station location in ascending order
            Arrays.sort(c);
            
            // Set max distance intially as the distance between city 0 to the first space station
            int maxDistance = c[0];
            
            // Calculate the max distance of the city between two space station
            for(int i = 1; i <c.length; i++){
                maxDistance = Math.max(maxDistance, (c[i] - c[i-1])/2);
            }
            
            // Calculate the distance between the last n city to the last space station
            maxDistance = Math.max(maxDistance, (n-1) - c[c.length-1]);
            
            return maxDistance;
        }