A sequence of integers is a one-sequence if the difference between any two consecutive numbers in this sequence is -1 or 1 and its first element is 0.
More precisely: \$a_1, a_2, ..., a_n\$ is a one-sequence if:
$$\forall k \::\: 1\le k<n, |a_k-a_{k+1}| = 1 \\ a_1 = 0$$
Input
- \$n\$ - number of elements in the sequence
- \$s\$ - sum of elements in the sequence
Output
- a one-sequence set/list/array/etc of length \$n\$ with sum of elements \$s\$, if possible
- an empty set/list/array/etc if not possible
Examples
For input 8 4
, output could be [0 1 2 1 0 -1 0 1]
or [0 -1 0 1 0 1 2 1]
. There may be other possibilites.
For input 3 5
, output is empty []
, since it cannot be done.
Rules
This is a code golf, shortest answer in bytes wins. Submissions should be a program or function. Input/output can be given in any of the standard ways.
(l-1)*l/2
and-(l-1)*l/2
which have the same parity as(l-1)*l/2
. \$\endgroup\$