Maximum distance to station

Looking at the following hackerrank problem: https://www.hackerrank.com/challenges/flatland-space-stations

You are given a number n where cities can be at indices 0 to n-1 and also given an unsorted list of indices of cities where there are stations. The goal is to find the maximum distance from a city to a station. For example, for n=5 and the station list [0,4], the answer would be 2 since index 2 is 2 away from both 0 and 4 and all other stations at indices 0-4 are closer to one of 0 or 4.

I was looking for a short Python3 solution. The function body takes in the number n and a list c (like [0,4] in the example). After some revisions, I came up with the 69 character function body below:

c.sort();return max(t-s for s,t in zip([-c]+c,c+[2*n-c[-1]-1]))//2

Curious about whether anyone can come up with a shorter body.

• Welcome to the site. Could you clarify how you are counting size? Your solution is not valid python on its own and doesn't fit any of our standard answer formats here. It is really important to know these parameters. Feb 17 '21 at 9:17
• I think this should also explain the problem itself a little more. Users should be able to understand the problem without having to go to some external site. Feb 17 '21 at 9:19
• You should maybe provide a standard function header that you want people to use. This will resolve issues around recursion and default arguments. Feb 17 '21 at 10:08
• 2*n-c[-1]-1 can be golfed to 2*n+~c[-1] as a start (relevant tip). :) And based on the // I assume we're using Python 3 (or 3.8) here? Feb 17 '21 at 12:01
• Is there any reason to limit this challenge to Python? Feb 17 '21 at 17:48

Distance solution:

Python 3, 52 Bytes

return max(min(abs(l-c)for l in s)for c in range(n))

Try it online!

Python 3, 59 bytes

return max((min(abs(l-c)for l in s),c)for c in range(n))

Try it online!

Edit: Switched back to Distance solution, Thanks to Jonathan Allan for -1 Byte

• Hmmm.... I'm questioning the validity of my results, the second test (on TIO) should be 100? I think I calculated the distance, not the index. Feb 17 '21 at 17:34
• Fixed! Added 8 characters. Feb 17 '21 at 17:46
• Excellent solution, I like your usage of the second list comprehension to avoid sorting and dividing by 2 and how you took advantage of tuples. To clarify, I was looking for a solution for maximum distance and I'm really impressed that you also found one for the index. Feb 17 '21 at 19:45
• You can remove the space in ) for. Feb 17 '21 at 20:36
• The reference implementation does return the distance rather than the index, so the 52 byter should be good. Feb 17 '21 at 20:42

Perl 5, 51 bytes

sub{max(map{$i=$_-1;min(map abs$i-$_,@_)}1..shift)}

Try it online!

R, 56 55 bytes

function(n,l)max(apply(abs(sapply(1:n-1,-,l)),2,min))

Try it online!

MATL, 8 bytes

:q-|X<X>

Try it at MATL Online

Explanation

% Implicitly grab the first input, N as an integer
:      % Create an array from 1...N
q      % Subtract one from each element to get a 0-based range: 0...N-1
-      % Implicitly grab the second input (a column vector) and subtract the
% the two, broadcasting this out into a matrix of distances between
% all cities and stations where each city is a column and the distance
% to each station is on each row
|      % Take the absolute value of the result
X<     % Compute the minimum value of each column (e.g. distance of each
% city to closest station)
X>     % Compute the maximum value of these distances (furthest city
% from any station)
% Implicitly display the result

Jelly, 6 bytes

ḶạṂ¥€Ṁ

Try it online!

5 bytes if you allow indexing from 1:

ạṂ¥€Ṁ

Explanation

Ḷ        [0..n-1]
€    Map
¥     (
ạ         Absolute difference [from each station position]
Ṃ        Minimum
¥     )
Ṁ   Maximum

Charcoal, 9 bytes

Ｉ⌈Ｅθ⌊↔⁻ηι

Try it online! Link is to verbose version of code. Explanation:

θ        First input
Ｅ         Map over implicit range
ι   Current value
η    Second input
↔⁻     Absolute difference
⌊       Minimum
⌈          Maximum
Ｉ           Cast to string
Implicitly print