N numbers closest to zero staying balanced
Pyth, 10 bytes
*-R/Q2Q%Q2
Try it online.
How it works
(implicit) Store the input in Q.
/Q2 Calculate Q/2 (integer division).
-R Q Subtract that value (-R) from each element in [0, ..., Q-1] (Q).
* %Q2 Repeat the resulting array Q%2 times.
Mathematica, 32 30 24 bytes
OddQ@#&&Range@#-(#+1)/2&
Code-golfing trick: The last argument to And
doesn't have to be a boolean.
APL, 16 15 13 bytes
Thanks to @Dennis for -2 bytes!
⌊(⍳⊢×2|⊢)-÷∘2
This is a monadic train that gives an empty array for even input. Below is the diagram:
┌────┴─────┐
⌊ ┌────────┼─┐
┌─┴─┐ - ∘
⍳ ┌─┼───┐ ┌┴┐
⊢ × ┌─┼─┐ ÷ 2
2 | ⊢
First, ⊢×2|⊢
gives the input times its mod 2; that is, odds will give themselves, and evens give 0. We use ⍳
to create a list of numbers from 1 to that (⍳0
gives the empty array), and then we subtract half the input and floor.