Using any programming language that supports functions, have the function


that take strArr which will be an an array consisting of the following elements:

  • N which will be the number of gas stations in a circular route
  • and each subsequent element will be the string g:c where
    • g is the amount of gas in gallons at that gas station and
    • c will be the amount of gallons of gas needed to get to the following gas station.

For example strArr may be:


Your goal is to return the index of the starting gas station that will allow you to travel around the whole route once, otherwise return the string "impossible".

For the example above there are 4 gas stations, and your program should return the string "1" because:

  • Starting at station 1 you receive 3 gallons of gas and spend 1 getting to the next station.

  • Then you have 2 gallons + 2 more at the next station and you spend 2 so you have 2 gallons when you get to the 3rd station.

  • You then have 3 but you spend 2 getting to the final station,

  • At the final station you receive 0 gallons and you spend your final gallon getting to your starting point.

Starting at any other gas station would make getting around the route impossible, so the answer is "1".

If there are multiple gas stations that are possible to start at, return the smallest index (of the gas station). N will be >= 2.

Correct Sample Outputs:

Input: ["4","1:1","2:2","1:2","0:1"]
Output: "impossible"

Input: ["4","0:1","2:2","1:2","3:1"]
Output: "4"

The winner will be the one to use the shortest code although it will be long.

  • 1
    \$\begingroup\$ What's the objective winning criterion? I'm voting to close because there's no way to tell who the winner is. \$\endgroup\$
    – Doorknob
    Feb 4, 2014 at 17:54
  • \$\begingroup\$ Just put a code-golf tag on it and it'll be ok. \$\endgroup\$
    – daniero
    Feb 4, 2014 at 18:10
  • 3
    \$\begingroup\$ Voting to close just because of a missing tag is a bit draconian, wouldn’t you say? Just add the tag. \$\endgroup\$
    – Timwi
    Feb 4, 2014 at 18:33
  • \$\begingroup\$ Are there always precisely N+1 elements in strArr? This would make N redundant in case the language already provides a way of getting the length of the array, right? (it'd still be useful for e.g. C though.) \$\endgroup\$
    – FireFly
    Feb 4, 2014 at 18:45
  • 2
    \$\begingroup\$ Two of the answers are interpreting the spec as requiring the name is_enough (9 chars); three are using a single-char name, and one doesn't name the function at all. Could you please clarify what is permissible? \$\endgroup\$ Feb 4, 2014 at 23:28

6 Answers 6


APL (70)



  • 1↓⍵: drop the first element (length), we don't need it
  • {...: for each of the gas stations...
    • ⎕ML←3: set ⎕ML to 3 within the inner function (changes behaviour of )
    • ⍵⊂⍨⍵≠':': split the string on :
    • ⍎¨: evaluate each part
    • -/: subtract the second number from the first (giving net effect for each gas station)
  • K←: store them in K
  • Z←⍳⍴K: get the indices for the gas station (1 to length of K), store in Z
  • ⌽∘K¨Z: rotate K by each value of Z, giving an array of arrays
  • ¯1⌽: rotate this array to the left by 1 (to put the unchanged one first instead of last)
  • +\¨: make a running sum for each inner array
  • 0∧.≤¨: for each running sum, see if there are negative values
  • Z/⍨: select from Z those elements for which the running sum had no negative values
  • ×⍴G←: store in G. If G has any elements:
  • :⊃G: return the first element of G.
  • ⋄'impossible': otherwise, return impossible.

Bash 178 170 161 157

Rather straight forward.

is_enough(){((t=(n=$1-1)<0))&&echo impossible||(x=(n ${@:3} $2);for z in ${@:2};do(((t+=${z%:*}-${z#*:})<0))&&is_enough ${x[*]}&&return;done;echo $[$#-++n])}

Spaced out:

is_enough() {
     ((t=(n=$1-1)<0)) && echo impossible || (
         x=(n ${@:3} $2);
         for z in ${@:2};do
             (((t+=${z%:*}-${z#*:})<0)) && is_enough ${x[*]} && return;
         echo $[$#-++n]

Ruby, 111

Here's a Ruby solution using eval:

"#{(1..s.size).find{|i|g=0;s.rotate(i-1).all?{|x|g+=eval x.tr ?:,?-;g>=0}}||:impossible}"}

Example usage:

is_enough[%w(4 3:1 2:2 1:2 0:1)] #=> "1"
is_enough[%w(4 1:1 2:2 1:2 0:1)] #=> "impossible"
is_enough[%w(4 0:1 2:2 1:2 3:1)] #=> "4"
is_enough[%w(4 1:2 2:1 4:3 0:1)] #=> "2"
is_enough[%w(4 0:1 2:2 1:2 3:1)] #=> "4"
is_enough[%w(4 0:1 0:1 4:1 0:1)] #=> "3"
is_enough[%w(8 0:1 0:1 4:1 0:1 2:1 3:1 0:0 0:1)] #=> "3"

EDIT: Corrected function name and return type.


Ruby 132

g=->a{n,*a=a.map{|e|e.split(?:).map &:to_i}


irb(main):003:0> g[["4","1:1","2:2","1:2","0:1"]]
=> "impossible"
irb(main):004:0> g[["4","0:1","2:2","1:2","3:1"]]
=> 4
  • \$\begingroup\$ Spec requires a string to be returned in all cases. \$\endgroup\$
    – boothby
    Feb 4, 2014 at 21:26
  • \$\begingroup\$ @boothby I can't find that requirement anywhere in the spec. Can you quote? \$\endgroup\$ Feb 4, 2014 at 22:01
  • 1
    \$\begingroup\$ For the example above there are 4 gas stations, and your program should return the string "1" because: \$\endgroup\$
    – boothby
    Feb 4, 2014 at 23:41
  • \$\begingroup\$ Ah screw it; Chron's solution is much shorter anyways :D \$\endgroup\$
    – daniero
    Feb 5, 2014 at 13:16

Python, 131

I find this one-liner fairly satisfying.

E=lambda s:([`i`for i in range(1,len(s)+1)if reduce(lambda r,t:r+eval(t[0]+'-'+t[-1])*(r>=0),s[i:]+s[1:i],0)>=0]+['impossible'])[0]
  • \$\begingroup\$ what is reduce() here? I am getting an error NameError: name 'reduce' is not defined \$\endgroup\$ Aug 18, 2019 at 3:33
  • \$\begingroup\$ @ErHarshRathore this is Python 2. In Python 3, reduce is still in the standard lib, but it was moved to the functools module. \$\endgroup\$
    – alkasm
    Aug 18, 2019 at 4:34
  • \$\begingroup\$ Yeah! I got it from functools import reduce thanks \$\endgroup\$ Aug 18, 2019 at 4:36
  • \$\begingroup\$ @ErHarshRathore Additionally the backticks `i` here is equivalent to str(i) in Python 3. \$\endgroup\$
    – alkasm
    Aug 18, 2019 at 4:37
  • \$\begingroup\$ Do you thing this post should be updated now to python 3.x? \$\endgroup\$ Aug 18, 2019 at 4:39

GolfScript (72 chars)


Online demo

This performs the obvious brute force approach. The most interesting bit IMO is the determination of whether the partial sums of an array of delta-fuel ever drops below 0:


This saves 1 char over the more obvious


If the output were indexed at 0 rather than at 1, an alternative approach to rotating the array would be preferable:


But this approach isn't easily adopted to indexing at 1:




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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