Is there a general formula to generate sequence 1, 2, 3, 3, 2, 1?

109 Views Asked by At

Lets say for length 5, I want to generate the following sequence:

1 2 3 2 1

but for length 6, I want to generate the following:

1 2 3 3 2 1

such that the sequence is palindromic and there is always a repeating middle for even length.

Is there a general f(n, m) where n = desired length of sequence that gives correct mth item from the sequence?

Other examples:

length 7: 1 2 3 4 3 2 1

length 8: 1 2 3 4 4 3 2 1

1

There are 1 best solutions below

0
On BEST ANSWER

You can use an expression such as:

$f(n, m) = \min(m, n+1-m)$ where $1 \le m \le n$.

The left part in the $\min$ function runs through the first $n$ positive integers in ascending order, the right part in descending order. Taking the least values gives your sequence.