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[0]]+c,c+[2*n-c[-1]-1]))//2

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

  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Feb 17 '21 at 9:17
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Feb 17 '21 at 9:19
  • 2
    \$\begingroup\$ You should maybe provide a standard function header that you want people to use. This will resolve issues around recursion and default arguments. \$\endgroup\$
    – Wheat Wizard
    Feb 17 '21 at 10:08
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Feb 17 '21 at 12:01
  • 3
    \$\begingroup\$ Is there any reason to limit this challenge to Python? \$\endgroup\$
    – xigoi
    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!

Index solution (added after my first two comments):

Python 3, 59 bytes

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

Try it online!

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

  • \$\begingroup\$ 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. \$\endgroup\$
    – M Virts
    Feb 17 '21 at 17:34
  • \$\begingroup\$ Fixed! Added 8 characters. \$\endgroup\$
    – M Virts
    Feb 17 '21 at 17:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – Bob Smith
    Feb 17 '21 at 19:45
  • \$\begingroup\$ You can remove the space in ) for. \$\endgroup\$ Feb 17 '21 at 20:36
  • \$\begingroup\$ The reference implementation does return the distance rather than the index, so the 52 byter should be good. \$\endgroup\$ 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


Try it online!


MATL, 8 bytes


Try it at MATL Online


       % 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:



ḶạṂ¥€Ṁ   Main dyadic link
Ḷ        [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
  E         Map over implicit range
        ι   Current value
       η    Second input
     ↔⁻     Absolute difference
    ⌊       Minimum
 ⌈          Maximum
I           Cast to string
            Implicitly print

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.