As part of a code that I want to implement more efficiently, I would like to find a closed form expression for the following sequence of length $N+L-1$ (the elements of the sequence are separated by comma):
$1,\, 2,\, 3,\, \ldots N-1,\, N,\, N,\, \ldots N,\, N,\, N-1,\, \ldots 3,\, 2,\, 1$
I have tried to find a relationship between the position of each number and its value by subtracting by some numbers, taking absolute values, dividing by a number and taking the integer part, but so far I haven't been able to get a closed form expression. Any hint or answer would be appreciated.
By closed form expression, I mean a formula that gives the value of the $k$-th term in the sequence given the positive integers $L$ and $N$. I am looking for an expression that is not defined by intervals, but that works for any value of $k$.
Let's start with the case $L \ge N$ so you do get a run of $N$s in the middle. You have a run of $N-1$ items from $1$ to $N-1$ at the start, a run of $N-1$ items at the end, so $N+L-1-(2N-2)=L-N+1\ N$'s in the center run. This suggests $$f(k)=\begin {cases} k&1 \le k \le N-1\\N&N\le k \le L\\L+N-k&k \gt L \end {cases}$$ If $L \lt N$ the run stops before it gets to $N$. The top number is $\frac 12(N+L-1)$, rounded up if the length is odd. If it is odd you have just one high number in the middle, while if it is even you have two. This suggests $$f(k)=\begin {cases} k&k\le \frac 12(N+L-1)\\N+L-k&k \ge \frac 12(N+L-1) \end {cases}$$