TITLE: A peculiar shortest path problem.
QUESTION [7 upvotes]: Consider the following problem.  You are given a positive integer $ N $.  Find the shortest path
$$
\langle a_0 = 0, a_1, a_2, a_3, \ldots , a_{m-1}, a_m = N \rangle
$$
where you start at $ a_0 = 0 $ and end at $ a_m = N $, where for your $ k^{th} $ step you must step exactly $ k $ units to the right or to the left.  ($ a_i \leq 0 $ is permissible). For example if $ N = 5 $ then the shortest path is $ \langle 0,-1,1,4,0,5 \rangle $ which we would say has a length of $ m = 5 $ since there were 5 steps.  The reader can convince themselves this is the shortest path for $ N = 5 $.

I am interested in finding a performant algorithm for generating a solution to the above problem.  I am also interested in deriving a sharp theoretical upper bound for the shortest path.  The algorithm's performance should be theoretically guaranteed, not based on an unproven heuristic.

I have observed the following trivial bounds:  The length $ m $ of the shortest path can be no larger than $ 2N-1 $, since there is a zig-zag path $ \langle 0, 1, -1, 2, -2, 3, -3, \ldots, N \rangle $ of length $ 2N - 1 $.  A trivial lower bound on $ m $ is $ f(N) $, where $ f(N) $ is the smallest integer $ k $ with $ k(k+1)/2 \geq N $.  Indeed, even a path where every step is taken in the positive direction can travel no farther away from the origin than $ 1 + 2 + 3 + \ldots + m = m(m+1)/2 $, so surely $ m $ has to be large enough that this quantity is at least as large as $ N $.  To summarize:
$$
\sqrt{2N} \approx f(N) \leq m \leq 2N - 1
$$
In practice, however, (and this isn't terribly surprising), for the numbers I have tested by a kind of semi-intelligent brute-force search, we find that the shortest paths are very close to the minimal length $ f(N) $.  However $ f(N)$ is not sharp.  You don't have to look far to see why.  Consider $ N = 2 $.  Evidently the shortest path for $ N = 2 $ is $ \langle 0, 1, -1, 2 \rangle $ which is $ m = 3 $ steps, which is 1 more than the lower bound $ f(2) = 2 $.
Nevertheless, the lower bound $ f(N) $ is quite good.  In fact, I just ran a program trying this problem for every $ N = 1, \ldots, 1000 $.  For each $ N $, I recorded the difference $ j(N) := m(N) - f(N) $, the difference between the actual shortest path length and the trivial lower bound.  Not only did I find that $ j(N) $ never seems to exceed $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might be possible to prove by induction that $ 0 \leq j(N) \leq 2 $, but I haven't been able to find the proof yet.  I suspect that for any $ N $, a shortest path can be found as follows:
Try the following code with m = f(N), f(N)+1, and f(N)+2:

let position = N
let stepSize = m
let path = []
while (stepSize > 0):
  push position onto beginning of path
  if position > 0:
    position = position - stepSize
  else:
    position = position + stepSize
  stepSize = stepSize - 1
return path

The problem is, all I can do right now is check $ m = f(N) $, $ m = f(N) + 1 $, and $ m = f(N) + 2 $ and use the first one that works.  This is a satisfactory $ O(\sqrt{N}) $ algorithm provided I can prove $ j(N) \in \{ 0, 1, 2 \} $ for all $ N $.  Is that even right?

REPLY [4 votes]: If you take $m$ steps, the total distance traveled is $\frac 12m(m+1)=T_m$, the $m^{th}$ triangular number.  If $N$ is less than this, you can take some steps leftward to decrease the final number.  You need to change a sum of $\frac 12\left(\frac 12m(m+1)-N)\right)$ from rightward to leftward to arrive at $N$.  It should be obvious you can always do this as long as the sum of the numbers is an integer, which means $\frac 12m(m+1)$ and $N$ are both even or both odd.
The index of the first triangular number greater than $N$ is $m=\left\lceil \frac 12 (\sqrt{1+8N}-1)\right\rceil$.  When $m \equiv 1,2 \pmod 4, T_m$ is odd.  Otherwise $T_m$ is even.  So figure the next triangular number above $N$, round up to get the correct parity, and you have the number of steps.